Every website has different type of content, but few things in common with any content and interesting thing is content has following parameters in common: (WordPress recognised and used the same)

  • Title
  • Content [description text] (post_content)
  • Type of the content (post_type)
  • Images for the content (attachment)
  • Content activity properties CRUD (Created by, time)
  • Status of the content (Visible, Draft, Pending)
  • Multiple properties related to this content (meta fields, child key:value pair)
  • Category or Tree level flags to identify content or relate the content with another content (Taxonomy, terms)
  • Backup

The content is recognised by the content_type (post_type) value. Post type will be anything like Event, Student, News etc. These content types are normally described as Post Types, which may be a little confusing for those they have never stored the different data in one table only based on the post_type value. Internally everything is stored in the one place; table name is wp_posts.

Default post types in wordpress:
1. Post (Post Type: ‘post’)
2. Page (Post Type: ‘page’)
3. Attachment (Post Type: ‘attachment’)
4. Revision (Post Type: ‘revision’)
5. Navigation menu (Post Type: ‘nav_menu_item’)

Interesting thing is awesome stores code as a wordpress post (post_content) with a custom post_type. The post revision feature of WP is works for awesome to make the code revisions.

Lets see how can we CRUD on post(s) and term(s)…

GETTERs:

Multiple Posts:

Single Post:

Fetch:

  • Where condition on multiple metas (relation OR, AND)
  • How to fetch post that DON’T have a meta key

LIKE operator/clause/condition:

Get meta:

Get term:

 

SETTERs:

Set Meta:

Attach taxonomy programmatically:

Get Pages (TODO: not tested)

Delete a post:

Trash a post:

Set multiple meta values in one go: