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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
[query.posts_builder part=start] { "post_type": "[aw2.get module.post_type default='post' /]", "post_status": "publish", "paged" : "[aw2.get request.paged default=1 /]", "orderby": "[aw2.get template.args.orderby default="menu_order"/]", "order": "[aw2.get template.args.order default="ASC"/]", "post_parent": "[aw2.get template.args.post_parent /]", "posts_per_page": [aw2.get template.args.posts_per_page default= -1 /] } [/query.posts_builder] //***Posts Builder Part Meta Query***// [query.posts_builder part=meta_query] { "relation":"or", "[aw2.get @meta_item.key /]":{ "key": "[aw2.get @meta_val.key /]", "value": "\"[aw2.get @meta_item.item]\"", "compare": "LIKE" } } [/query.posts_builder] //***Posts Builder Part Meta Query for single meta***// [query.posts_builder part=meta_query] { "key": "[aw2.get @meta_val.key /]", "value": "[aw2.get @meta_val.item.value /]", "compare": "[aw2.get @meta_val.item.compare default='=' /]", "type": "[aw2.get @meta_val.item.type default='CHAR' /]" } [/query.posts_builder] //***Posts Builder Part Taxo Query***// [query.posts_builder part=tax_query not_empty="{@taxo_val.item.terms}"] { "taxonomy": "[aw2.get @taxo_val.key /]", "field": "[aw2.get @taxo_val.item.field default="ids" /]", "terms": [ [aw2.get @taxo_val.item.terms /] ], "include_children": [aw2.get @taxo_val.item.include_children default=true /] } [/query.posts_builder] [query.posts_builder part=run set="template.result" /] |
Single Post:
1 |
[query.get_post post_slug="hello" post_type="post" set='template.result' /] |
Fetch:
- Where condition on multiple metas (relation OR, AND)
- How to fetch post that DON’T have a meta key
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
[query.posts_builder part=start] { "posts_per_page": 10, "post_type": "product", "post_status": "publish", "order": "DESC", "orderby": "date" } [/query.posts_builder] [query.posts_builder part=tax_query] { "taxonomy": "product_category", "field": "slug", "terms": "us-ndc" } [/query.posts_builder] [query.posts_builder posts_builder part=meta_query] { "relation": "AND", 0: { "key": "set_id", "value": " ", "compare": "!=" }, 1: { "key": "product_packaging", "compare": "NOT EXISTS" } } [/query.posts_builder] [query.posts_builder part=run set=template.products /] |
LIKE operator/clause/condition:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[query.posts_builder part=start] { "posts_per_page": 1, "post_type": "product", "post_parent":0, "post_status": "publish", "order": "DESC", "orderby": "title" } [/query.posts_builder] [query.posts_builder part=meta_query] { "key": "meta_key", "value" : "[aw2.get variable_value /]", "compare" : "LIKE" } [/query.posts_builder] [query.posts_builder part="run" set="template.results" /] |
Get meta:
1 2 3 4 5 6 7 8 |
[query.get_post_meta post_id=1 key='custom_value2' set='template.result' /] ID: template.result.ID Meta: template.result.<strong>meta</strong>.any_meta_key Term: template.result.taxonomy.slugs.product_category In loop: Meta: [aw2.get @product.item.meta.ndc_number dump=t /] Terms: [aw2.get @product.item.taxonomy.slugs.product_category dump=t /] |
Get term:
1 |
[query.get_post_terms post_id=1 taxonomy=category <any args> set='template.result' /] |
1 2 3 |
[query.get_post_terms post_id=1 taxonomy=category set='template.result'] args JSON Object [/query] |
1 |
[query.get_post_terms post_id=1 taxonomy=category fields=names set='template.result' /] |
1 |
[query.get_term_by field="slug" value="text_term_slug" taxonomy="any_taxo" set="module.result" /] |
SETTERs:
1 2 |
Append single or multiple the terms by id: query.set_post_terms post_id=1 taxonomy=category terms='10,12' append=1 /] |
Set Meta:
1 |
[query.update_post_meta post_id=1 meta_key='new_key' meta_value='new_value' /] |
Attach taxonomy programmatically:
1 |
[query.set_post_terms post_id=<id> taxonomy='<tax>' slugs='<comma Separated slugs>'/] |
Get Pages (TODO: not tested)
1 2 3 4 5 |
[query.get_pages set=result post_status=publish] { "post_type":"post" } [/query] |
Delete a post:
1 |
[query.delete_post post_id=<id> force_delete /] |
Trash a post:
1 |
[query.trash_post post_id=<id> /] |
Set multiple meta values in one go:
1 2 3 4 5 6 7 |
[query.post_set_meta post_id=<id>] { "key";"value", "key";"value", "key";"value" } [/query] |