WordPress order by post type

Post Types Order

Description

Over 6.8 MILLIONS DOWNLOADS and near PERFECT rating out of 200 REVIEWS.
A powerful plugin, Order Posts and Post Types Objects using a Drag and Drop Sortable JavaScript capability.

The order can be customized within default WordPress post type archive list page or a separate Re-Order interface which displays all objects.
It allows reordering the posts for any custom post types you defined, including the default Posts. Post Order has never been easier.

Usage

This plugin was developed considering everyone to be able to use the sorting feature, no matter the WordPress experience:

  • Install the plugin through the Install Plugins interface or by uploading the post-types-order folder to your /wp-content/plugins/ directory.
  • Activate the Post Order plugin.
  • A new setting page will be created within Settings > Post Types Order, you should check with that, and make a first options save.
  • Using the AutoSort option as ON you don’t need to worry about any code changes, the plugin will apply the customized post order on fly.
  • Use the Re-Order interface which appears to every custom post type (non-hierarchical) to change the post order to a new one.
  • If prefer sort apply through the code, include ‘orderby’ =>’menu_order’ within custom query arguments, more details at http://www.nsp-code.com/sample-code-on-how-to-apply-the-sort-for-post-types-order-plugin/

Example of Usage

As you can see just a matter of drag and drop and post ordering will change on front side right away.
If for some reason the post order does not update on your front side, you either do something wrong or the theme code you are using does not use a standard query per WordPress Codex rules and regulations. But we can still help, use the forum to report your issue as there are many peoples who gladly help or get in touch with us.

Something is wrong with this plugin on your site? Just use the forum or get in touch with us at Contact and we’ll check it out.

Need More? Check out the advanced version of this plugin at Advanced Post Types Order which include Hierarchically post types order, Manual / Automatic Sorting, Individual Categories Order, Conditionals to apply, Paginations for large list, Mobile ready, Enhanced Interface, Plugins compatibility (MultiSite Network Support, WPML, Polylang, WooCommerce, WP E-Commerce, Platform Pro, Genesis etc), font side re-order interface, … and many more !!

This plugin is developed by Nsp-Code

Localization

Want to contribute with a translation to your language? Please check at https://translate.wordpress.org/projects/wp-plugins/post-types-order
http://www.nsp-code.com

Screenshots

The ReOrder interface through which the sort can be created.

Sort can be managed within default WordPress post type interface.

Installation

  1. Upload post-types-order folder to your /wp-content/plugins/ directory.
  2. Activate the plugin from Admin > Plugins menu.
  3. Once activated you should check with Settings > Post Types Order
  4. Use Re-Order link which appear into each post type section or use default WordPress interface to make your sorting.

Feel free to contact us at electronice_delphi@yahoo.com

Since I have no PHP knowledge at all, is this plugin for me?

Absolutely, you can!
Unlike many other plugins, you don’t have to do any code changes to make your post order to apply, accordingly to custom-defined order. There is an option to auto-update the WordPress queries so the posts will be returned in customised order. This can be turned off (Autosort) to allow customized code usage.

How to manually apply the sort on queries

Include a ‘orderby’ => ‘menu_order’ property within your custom query arguments.

What kind of posts/pages does this plugin allow me to sort?

You can sort ALL post types that you have defined into your WordPress as long they are not hierarhically defined: Posts (default WordPress custom post type), Movies, Reviews, Data etc..

Ok, i understand about the template post types order, how about the admin interface?

There’s an option you can trigger, to see the post types order as you defined in the sort list, right into the main admin post list interface.

There is a feature that i want it implemented, can you do something about it?

All ideas are welcome and i put them on my list to be implemented into the new versions. Anyway this may take time, but if you are in a rush, please consider a small donation and we can arrange something.

Can i make certain queries to ignore the custom sort when Autosort is turned On?

This can be done by including the ignore_custom_sort within custom query arguments. An example can be found at http://www.nsp-code.com/advanced-post-types-order-api/sample-usage/

How can i force the sort to apply for certain queries when AutoSort is turned On?

A filter can be used to achieve that pto/posts_orderby. An example can be found at http://www.nsp-code.com/ignore-sort-apply-for-certain-query-on-post-types-order/

I still need more features like front sorting interface, shortcodes, filters, conditionals, advanced queries, taxonomy/ category sorting etc

Consider upgrading to our advanced version of this plugin at a very resonable price Advanced Post Types Order

Источник

Developer Resources

The WordPress Query class.

Contents

More Information

Most of the time you can find the information you want without actually dealing with the class internals and global variables. There are a whole bunch of functions that you can call from anywhere that will enable you to get the information you need.

There are two main scenarios you might want to use WP_Query in. The first is to find out what type of request WordPress is currently dealing with. The $is_* properties are designed to hold this information: use the Conditional Tags to interact here. This is the more common scenario to plugin writers (the second normally applies to theme writers).

The second is during The Loop. WP_Query provides numerous functions for common tasks within The Loop. To begin with, have_posts() , which calls $wp_query->have_posts() , is called to see if there are any posts to show. If there are, a while loop is begun, using have_posts() as the condition. This will iterate around as long as there are posts to show. In each iteration, the_post() , which calls $wp_query->the_post() is called, setting up internal variables within $wp_query and the global $post variable (which the Template Tags rely on), as above. These are the functions you should use when writing a theme file that needs a loop.

Note: If you use the_post() with your query, you need to run wp_reset_postdata() afterwards to have template tags use the main query’s current post again.

Usage

Standard Loop

Standard Loop (Alternate)

Multiple Loops

If you have multiple queries, you need to perform multiple loops. Like so…

Properties and Methods

This is the formal documentation of WP_Query . You shouldn’t alter the properties directly, but instead use the methods (see methods list below) to interact with them.

Properties

  • $query
    Holds the query string that was passed to the $wp_query object by WP class.
  • $query_vars
    An associative array containing the dissected $query : an array of the query variables and their respective values.
  • $queried_object
    Applicable if the request is a category, author, permalink or Page. Holds information on the requested category, author, post or Page.
  • $queried_object_id
    If the request is a category, author, permalink or post / page, holds the corresponding ID.
  • $posts
    Gets filled with the requested posts from the database.
  • $post_count
    The number of posts being displayed.
  • $found_posts
    The total number of posts found matching the current query parameters
  • $max_num_pages
    The total number of pages. Is the result of $found_posts / $posts_per_page
  • $current_post
    (available during The Loop) Index of the post currently being displayed.
  • $post
    (available during The Loop) The post currently being displayed.
  • $is_single , $is_page , $is_archive , $is_preview , $is_date , $is_year , $is_month , $is_time , $is_author , $is_category , $is_tag , $is_tax , $is_search , $is_feed , $is_comment_feed , $is_trackback , $is_home , $is_404 , $is_comments_popup , $is_admin , $is_attachment , $is_singular , $is_robots , $is_posts_page , $is_paged
    Booleans dictating what type of request this is. For example, the first three represent ‘is it a permalink?’, ‘is it a Page?’, ‘is it any type of archive page?’, respectively. See also Conditional Tags.
Читайте также:  Linux rpm удаление пакета

Parameters

Author Parameters

Show posts associated with certain author.

  • author (int) – use author id.
  • author_name (string) – use ‘ user_nicename ‘ – NOT name.
  • author__in (array) – use author id (available since version 3.7).
  • author__not_in (array) – use author id (available since version 3.7).

Show Posts for one Author

Display posts by author, using author id:

Display posts by author, using author ‘ user_nicename ‘:

Show Posts From Several Authors

Display posts from several specific authors:

Exclude Posts Belonging to an Author

Display all posts except those from an author(singular) by prefixing its id with a ‘-‘ (minus) sign:

Multiple Author Handling

Display posts from multiple authors:

You can also exclude multiple author this way:

Category Parameters

Show posts associated with certain categories.

  • cat (int) – use category id.
  • category_name (string) – use category slug.
  • category__and (array) – use category id.
  • category__in (array) – use category id.
  • category__not_in (array) – use category id.

Display posts that have one category (and any children of that category), using category id:

Display posts that have this category (and any children of that category), using category slug:

Display posts that have this category (not children of that category), using category id:

Display posts that have several categories, using category id:

Display posts that have these categories, using category slug:

Display posts that have “all” of these categories:

Display all posts except those from a category by prefixing its id with a ‘-‘ (minus) sign.

Display posts that are in multiple categories. This shows posts that are in both categories 2 and 6:

To display posts from either category 2 OR 6, you could use cat as mentioned above, or by using category__in (note this does not show posts from any children of these categories):

You can also exclude multiple categories this way:

Tag Parameters

Show posts associated with certain tags.

  • tag (string) – use tag slug.
  • tag_id (int) – use tag id.
  • tag__and (array) – use tag ids.
  • tag__in (array) – use tag ids.
  • tag__not_in (array) – use tag ids.
  • tag_slug__and (array) – use tag slugs.
  • tag_slug__in (array) – use tag slugs.

Display posts that have one tag, using tag slug:

Display posts that have this tag, using tag id:

Display posts that have “either” of these tags:

Display posts that have “all” of these tags:

Display posts that are tagged with both tag id 37 and tag id 47:

To display posts from either tag id 37 or 47, you could use tag as mentioned above, or explicitly specify by using tag__in :

Display posts that do not have any of the two tag ids 37 and 47:

The tag_slug__in and tag_slug__and behave much the same, except match against the tag’s slug.

Taxonomy Parameters

Show posts associated with certain taxonomy.

  • (string) – use taxonomy slug. (Deprecated since version 3.1 in favor of ‘ tax_query ‘).
  • tax_query (array) – use taxonomy parameters (available since version 3.1).
    • relation (string) – The logical relationship between each inner taxonomy array when there is more than one. Possible values are ‘AND’, ‘OR’. Do not use with a single inner taxonomy array.
      • taxonomy (string) – Taxonomy.
      • field (string) – Select taxonomy term by. Possible values are ‘term_id’, ‘name’, ‘slug’ or ‘term_taxonomy_id’. Default value is ‘term_id’.
      • terms (int/string/array) – Taxonomy term(s).
      • include_children (boolean) – Whether or not to include children for hierarchical taxonomies. Defaults to true.
      • operator (string) – Operator to test. Possible values are ‘IN’, ‘NOT IN’, ‘AND’, ‘EXISTS’ and ‘NOT EXISTS’. Default value is ‘IN’.

Important Note: tax_query takes an array of tax query arguments arrays (it takes an array of arrays).
This construct allows you to query multiple taxonomies by using the relation parameter in the first (outer) array to describe the boolean relationship between the taxonomy arrays.

Simple Taxonomy Query:

Display posts tagged with bob, under people custom taxonomy:

Multiple Taxonomy Handling:

Display posts from several custom taxonomies:

Display posts that are in the quotes category OR have the quote post format:

Nested Taxonomy Handling:

The ‘tax_query’ clauses can be nested, to create more complex queries. Example: Display posts that are in the quotes category OR both have the quote post format AND are in the wisdom category:

Search Parameters

Show posts based on a keyword search.

  • s (string) – Search keyword.

Show Posts based on a keyword search

Prepending a term with a hyphen will exclude posts matching that term. Eg, ‘pillow -sofa’ will return posts containing ‘pillow’ but not ‘sofa’ (available since Version 4.4).

Post & Page Parameters

Display content based on post and page parameters. Remember that default post_type is only set to display posts but not pages.

  • p (int) – use post id.
  • name (string) – use post slug.
  • page_id (int) – use page id.
  • pagename (string) – use page slug.
  • post_parent (int) – use page id to return only child pages. Set to 0 to return only top-level entries.
  • post_parent__in (array) – use post ids. Specify posts whose parent is in an array (available since version 3.6).
  • post_parent__not_in (array) – use post ids. Specify posts whose parent is not in an array (available since version 3.6).
  • post__in (array) – use post ids. Specify posts to retrieve. ATTENTION If you use sticky posts, they will be included (prepended!) in the posts you retrieve whether you want it or not. To suppress this behaviour use ignore_sticky_posts .
  • post__not_in (array) – use post ids. Specify post NOT to retrieve.
  • post_name__in (array) – use post slugs. Specify posts to retrieve (available since version 4.4).

NOTE: Ticket #28099: Passing an empty array to post__in will return has_posts() as true (and all posts will be returned). Logic should be used before hand to determine if WP_Query should be used in the event that the array being passed to post__in is empty.

Display post by ID:

Display page by ID:

Show post/page by slug

Display page by slug :

Display child page using the slug of the parent and the child page, separated by a slash (e.g. ‘parent_slug/child_slug’):

Display child pages using parent page ID:

Display only top-level pages, exclude all child pages:

Display posts whose parent is in an array:

Display only the specific posts:

Display all posts but NOT the specified ones:

Note: you cannot combine post__in and post__not_in in the same query.

Also note that using a string containing a comma separated list will not work here. If you’re passing a variable, make sure it’s a proper array of integer values:

Password Parameters

Show content based on post and page parameters. Remember that default post_type is only set to display posts but not pages.

  • has_password (bool) – true for posts with passwords ; false for posts without passwords ; null for all posts with and without passwords (available since version 3.9).
  • post_password (string) – show posts with a particular password (available since version 3.9)

Display only password protected posts:

Display only posts without passwords:

Display only posts with and without passwords:

Display posts with a particular password:

Post Type Parameters

Show posts associated with certain type.

  • post_type (string / array) – use post types. Retrieves posts by post types, default value is ‘ post ‘. If ‘ tax_query ‘ is set for a query, the default value becomes ‘ any ‘;
    • ‘ post ‘ – a post.
    • ‘ page ‘ – a page.
    • ‘ revision ‘ – a revision.
    • ‘ attachment ‘ – an attachment. Whilst the default WP_Query post_status is ‘publish’, attachments have a default post_status of ‘inherit’. This means no attachments will be returned unless you also explicitly set post_status to ‘inherit’ or ‘any’. See Status parameters section below.
    • ‘ nav_menu_item ‘ – a navigation menu item
    • ‘ any ‘ – retrieves any type except revisions and types with ‘exclude_from_search’ set to true.
      ** Custom Post Types (e.g. movies)

Display only pages:

Display ‘ any ‘ post type (retrieves any type except revisions and types with ‘exclude_from_search’ set to TRUE):

Display multiple post types, including custom post types:

Status Parameters

Show posts associated with certain post status.

Display only posts with the ‘ draft ‘ status:

Display multiple post status:

Display all attachments:

Comment Parameters

Since Version 4.9 Introduced the `$comment_count` parameter. It can be either an Integer or an Array.

  • comment_count (int) – The amount of comments your CPT has to have ( Search operator will do a ‘=’ operation )
  • comment_count (Array) – If comment_count is an array, it should have two arguments:
    • ‘ value ‘ – The amount of comments your post has to have when comparing
    • ‘ compare ‘ – The search operator. Possible values are ‘=’, ‘!=’, ‘>’, ‘>=’, ‘

    Pagination Parameters

    • nopaging (boolean) – show all posts or use pagination. Default value is ‘false’, use paging.
    • posts_per_page (int) – number of post to show per page (available since version 2.1, replaced showposts parameter). Use ‘posts_per_page’=>-1 to show all posts (the ‘offset’ parameter is ignored with a -1 value). Set the ‘paged’ parameter if pagination is off after using this parameter. Note: if the query is in a feed, wordpress overwrites this parameter with the stored ‘posts_per_rss’ option. To reimpose the limit, try using the ‘post_limits’ filter, or filter ‘pre_option_posts_per_rss’ and return -1
    • posts_per_archive_page (int) – number of posts to show per page – on archive pages only. Over-rides posts_per_page and showposts on pages where is_archive() or is_search() would be true.
    • offset (int) – number of post to displace or pass over. Warning: Setting the offset parameter overrides/ignores the paged parameter and breaks pagination. The ‘offset’ parameter is ignored when ‘posts_per_page’=>-1 (show all posts) is used.
    • paged (int) – number of page. Show the posts that would normally show up just on page X when using the “Older Entries” link.
    • page (int) – number of page for a static front page. Show the posts that would normally show up just on page X of a Static Front Page.
    • ignore_sticky_posts (boolean) – ignore post stickiness (available since version 3.1, replaced caller_get_posts parameter). false (default): move sticky posts to the start of the set. true : do not move sticky posts to the start of the set.

    Display x posts per page:

    Display all posts in one page:

    Display all posts by disabling pagination:

    Display posts from the 4th one:

    Display 5 posts per page which follow the 3 most recent posts:

    Display posts from page number x:

    Display posts from current page:

    Display posts from the current page and set the ‘paged’ parameter to 1 when the query variable is not set (first page).

    Pagination Note: Use get_query_var(‘page’); if you want your query to work in a page template that you’ve set as your static front page. The query variable ‘page’ also holds the pagenumber for a single paginated Post or Page that includes the quicktag in the post content.

    Display posts from current page on a static front page:

    Display just the first sticky post:

    Display just the first sticky post, if none return the last post published:

    Display just the first sticky post, if none return nothing:

    Exclude all sticky posts from the query:

    Exclude sticky posts from a category:

    Return ALL posts within the category, but don’t show sticky posts at the top. The ‘sticky posts’ will still show in their natural position (e.g. by date):

    Exclude sticky posts from a category:

    Return posts within the category, but exclude sticky posts completely, and adhere to paging rules:

    Order & Orderby Parameters

    Sort retrieved posts.

    • order (string | array) – Designates the ascending or descending order of the ‘ orderby ‘ parameter. Defaults to ‘DESC’. An array can be used for multiple order/orderby sets.
      • ‘ ASC ‘ – ascending order from lowest to highest values (1, 2, 3; a, b, c).
      • ‘ DESC ‘ – descending order from highest to lowest values (3, 2, 1; c, b, a).
    • orderby (string | array) – Sort retrieved posts by parameter. Defaults to ‘date (post_date)’. One or more options can be passed.
      • ‘ none ‘ – No order (available since version 2.8).
      • ‘ ID ‘ – Order by post id. Note the capitalization.
      • ‘ author ‘ – Order by author.
      • ‘ title ‘ – Order by title.
      • ‘ name ‘ – Order by post name (post slug).
      • ‘ type ‘ – Order by post type (available since version 4.0).
      • ‘ date ‘ – Order by date.
      • ‘ modified ‘ – Order by last modified date.
      • ‘ parent ‘ – Order by post/page parent id.
      • ‘ rand ‘ – Random order.
      • ‘ comment_count ‘ – Order by number of comments (available since version 2.9).
      • ‘ relevance ‘ – Order by search terms in the following order: First, whether the entire sentence is matched. Second, if all the search terms are within the titles. Third, if any of the search terms appear in the titles. And, fourth, if the full sentence appears in the contents.
      • ‘ menu_order ‘ – Order by Page Order. Used most often for pages (Order field in the Edit Page Attributes box) and for attachments (the integer fields in the Insert / Upload Media Gallery dialog), but could be used for any post type with distinct ‘ menu_order ‘ values (they all default to 0 ).
      • ‘ meta_value ‘ – Note that a ‘ meta_key=keyname ‘ must also be present in the query. Note also that the sorting will be alphabetical which is fine for strings (i.e. words), but can be unexpected for numbers (e.g. 1, 3, 34, 4, 56, 6, etc, rather than 1, 3, 4, 6, 34, 56 as you might naturally expect). Use ‘ meta_value_num ‘ instead for numeric values. You may also specify ‘ meta_type ‘ if you want to cast the meta value as a specific type. Possible values are ‘NUMERIC’, ‘BINARY’, ‘CHAR’, ‘DATE’, ‘DATETIME’, ‘DECIMAL’, ‘SIGNED’, ‘TIME’, ‘UNSIGNED’, same as in ‘ $meta_query ‘. When using ‘meta_type’ you can also use ‘meta_value_*’ accordingly. For example, when using DATETIME as ‘meta_type’ you can use ‘meta_value_datetime’ to define order structure.
      • ‘ meta_value_num ‘ – Order by numeric meta value (available since version 2.8). Also note that a ‘ meta_key=keyname ‘ must also be present in the query. This value allows for numerical sorting as noted above in ‘ meta_value ‘.
      • ‘ post__in ‘ – Preserve post ID order given in the post__in array (available since version 3.5). Note – the value of the order parameter does not change the resulting sort order.
      • ‘ post_name__in ‘ – Preserve post slug order given in the ‘post_name__in’ array (available since Version 4.6). Note – the value of the order parameter does not change the resulting sort order.
      • ‘ post_parent__in ‘ -Preserve post parent order given in the ‘post_parent__in’ array (available since Version 4.6). Note – the value of the order parameter does not change the resulting sort order.

    Display posts sorted by post ‘title’ in a descending order:

    Display posts sorted by ‘menu_order’ with a fallback to post ‘title’, in a descending order:

    Display one random post:

    Display posts ordered by comment count (popularity):

    Display posts with ‘Product’ type ordered by ‘Price’ custom field:

    Display pages ordered by ‘title’ and ‘menu_order’. (title is dominant):

    Display pages ordered by ‘title’ and ‘menu_order’ with different sort orders (ASC/DESC) (available since version 4.0):

    Mulitiple orderby/order pairs

    ‘orderby’ with ‘meta_value’ and custom post type

    Display posts of type ‘my_custom_post_type’, ordered by ‘age’, and filtered to show only ages 3 and 4 (using meta_query).

    ‘orderby’ with multiple ‘meta_key’s

    If you wish to order by two different pieces of postmeta (for example, City first and State second), you need to combine and link your meta query to your orderby array using ‘named meta queries’. See the example below:

    (props to cybmeta on WPSE for this example).

    Date Parameters

    Show posts associated with a certain time and date period.

    • year (int) – 4 digit year (e.g. 2011).
    • monthnum (int) – Month number (from 1 to 12).
    • w (int) – Week of the year (from 0 to 53). Uses MySQL WEEK command. The mode is dependent on the “start_of_week” option.
    • day (int) – Day of the month (from 1 to 31).
    • hour (int) – Hour (from 0 to 23).
    • minute (int) – Minute (from 0 to 60).
    • second (int) – Second (0 to 60).
    • m (int) – YearMonth (For e.g.: 201307 ).
    • date_query (array) – Date parameters (available since version 3.7).
      • year (int) – 4 digit year (e.g. 2011).
      • month (int) – Month number (from 1 to 12).
      • week (int) – Week of the year (from 0 to 53).
      • day (int) – Day of the month (from 1 to 31).
      • hour (int) – Hour (from 0 to 23).
      • minute (int) – Minute (from 0 to 59).
      • second (int) – Second (0 to 59).
      • after (string/array) – Date to retrieve posts after. Accepts strtotime() -compatible string, or array of ‘year’, ‘month’, ‘day’ values:
        • year (string) Accepts any four-digit year. Default is empty.
        • month (string) The month of the year. Accepts numbers 1-12. Default: 12.
        • day (string) The day of the month. Accepts numbers 1-31. Default: last day of month.
      • before (string/array) – Date to retrieve posts before. Accepts strtotime() -compatible string, or array of ‘year’, ‘month’, ‘day’ values:
        • year (string) Accepts any four-digit year. Default is empty.
        • month (string) The month of the year. Accepts numbers 1-12. Default: 1.
        • day (string) The day of the month. Accepts numbers 1-31. Default: 1.
      • inclusive (boolean) – For after/before, whether exact value should be matched or not’.
      • compare (string) – See WP_Date_Query::get_compare().
      • column (string) – Posts column to query against. Default: ‘post_date’.
      • relation (string) – OR or AND, how the sub-arrays should be compared. Default: AND.

    Returns posts dated December 12, 2012:

    Returns posts for today:

    Returns posts for this week:

    Return posts between 9AM to 5PM on weekdays

    Return posts from January 1st to February 28th

    Note that if a strtotime() -compatible string with just a date was passed in the before parameter, this will be converted to 00:00:00 on that date. In this case, even if inclusive was set to true, the date would not be included in the query. If you want a before date to be inclusive, include the time as well, such as ‘before’ => ‘2013-02-28 23:59:59’ , or use the array format, which is adjusted automatically if inclusive is set.

    Return posts made over a year ago but modified in the past month

    The ‘date_query’ clauses can be nested, in order to construct complex queries. See Taxonomy Parameters for details on the syntax.

    Custom Field (post meta) Parameters

    Show posts associated with a certain custom field.

    This part of the query is parsed by WP_Meta_Query, so check the docs for it as well in case this list of arguments isn’t up to date.

    • meta_key (string) – Custom field key.
    • meta_value (string) – Custom field value.
    • meta_value_num (number) – Custom field value.
    • meta_compare (string) – Operator to test the ‘ meta_value ‘. Possible values are ‘=’, ‘!=’, ‘>’, ‘>=’, ‘ meta_query (array) – Custom field parameters (available since version 3.1).
      • relation (string) – The logical relationship between each inner meta_query array when there is more than one. Possible values are ‘AND’, ‘OR’. Do not use with a single inner meta_query array.

    meta_query also contains one or more arrays with the following keys:

    • key (string) – Custom field key.
    • value (string|array) – Custom field value. It can be an array only when compare is ‘IN’ , ‘NOT IN’ , ‘BETWEEN’ , or ‘NOT BETWEEN’ . You don’t have to specify a value when using the ‘EXISTS’ or ‘NOT EXISTS’ comparisons in WordPress 3.9 and up.
      (Note: Due to bug #23268, value is required for NOT EXISTS comparisons to work correctly prior to 3.9. You must supply some string for the value parameter. An empty string or NULL will NOT work. However, any other string will do the trick and will NOT show up in your SQL when using NOT EXISTS . Need inspiration? How about ‘bug #23268’ .)
    • compare (string) – Operator to test. Possible values are ‘=’, ‘!=’, ‘>’, ‘>=’, ‘ type (string) – Custom field type. Possible values are ‘NUMERIC’, ‘BINARY’, ‘CHAR’, ‘DATE’, ‘DATETIME’, ‘DECIMAL’, ‘SIGNED’, ‘TIME’, ‘UNSIGNED’. Default value is ‘CHAR’.

    The ‘type’ DATE works with the ‘compare’ value BETWEEN only if the date is stored at the format YYYY-MM-DD and tested with this format.

    Important Note: meta_query takes an array of meta query arguments arrays (it takes an array of arrays) – you can see this in the examples below.
    This construct allows you to query multiple metadatas by using the relation parameter in the first (outer) array to describe the boolean relationship between the meta queries. Accepted arguments are ‘AND’, ‘OR’. The default is ‘AND’.

    Simple Custom Field Query:

    Display posts where the custom field key is ‘color’, regardless of the custom field value:

    Display posts where the custom field value is ‘blue’, regardless of the custom field key:

    Display page where the custom field value is ‘blue’, regardless of the custom field key:

    Display posts where the custom field key is ‘color’ and the custom field value is ‘blue’:

    Display posts where the custom field key is ‘color’ and the custom field value IS NOT ‘blue’:

    Display posts where the custom field key is a set date and the custom field value is now. Displays only posts which date has not passed.

    Display ‘product'(s) where the custom field key is ‘price’ and the custom field value that is LESS THAN OR EQUAL TO 22.
    By using the ‘meta_value’ parameter the value 99 will be considered greater than 100 as the data are stored as ‘strings’, not ‘numbers’. For number comparison use ‘meta_value_num’.

    Display posts with a custom field value of zero (0), regardless of the custom field key:

    Display posts from a single custom field:

    (Note that meta_query expects nested arrays, even if you only have one query.)

    Display posts from several custom fields:

    Display posts that have meta key ‘color’ NOT LIKE value ‘blue’ OR meta key ‘price’ with values BETWEEN 20 and 100:

    The ‘meta_query’ clauses can be nested in order to construct complex queries. For example, show productss where color=orange OR color=red&size=small translates to the following:

    Permission Parameters

    Show posts if user has the appropriate capability

    • perm (string) – User permission.

    Display published and private posts, if the user has the appropriate capability:

    Mime Type Parameters

    Used with the attachments post type.

    • post_mime_type (string/array) – Allowed mime types.

    Get attachments that are gif images:

    Get gif images and remember that by default the attachment’s post_status is set to inherit.

    Get attachments that are not images:

    Caching Parameters

    Stop the data retrieved from being added to the cache.

    • cache_results (boolean) – Post information cache.
    • update_post_meta_cache (boolean) – Post meta information cache.
    • update_post_term_cache (boolean) – Post term information cache.

    Show Posts without adding post information to the cache

    Display 50 posts, but don’t add post information to the cache:

    Show Posts without adding post meta information to the cache

    Display 50 posts, but don’t add post meta information to the cache:

    Show Posts without adding post term information to the cache

    Display 50 posts, but don’t add post term information to the cache:

    In general usage you should not need to use these, adding to the cache is the right thing to do, however they may be useful in specific circumstances. An example of such circumstances might be when using a WP_Query to retrieve a list of post titles and URLs to be displayed, but in which no other information about the post will be used and the taxonomy and meta data won’t be needed. By not loading this information, you can save time from the extra unnecessary SQL queries.

    Note: If a persistent object cache backend (such as memcached) is used, these flags are set to false by default since there is no need to update the cache every page load when a persistent cache exists.

    Return Fields Parameter

    Set return values.

    • fields (string) – Which fields to return. There are three options:
      • ‘all’ – Return all fields (default).
      • ‘ids’ – Return an array of post IDs.
      • ‘id=>parent’ – Return an array of stdClass objects with ID and post_parent properties.

    Passing anything else will return all fields (default) – an array of post objects.

    Источник

Поделиться с друзьями
КомпСовет
Adblock
detector