Wednesday, 18-05-2022

How to get post data in WordPress?

WordPress provides a number of useful utility functions used to get post data. Post data is understood as the data of a specific article such as ID, post content, thumbnail, author, … or it can also be an array of articles that meet one or more certain conditions.

First, we will look at how to get the content of the post based on the ID, using the get_post() function
The return value of this function will be an array or an object.
//return object
//return array type: Returns an associative array of field names to values
//returns a numeric array of field values

To learn more, please read the article get post by id

Get posts that match your criteria

3 common functions to get posts are: get_posts(), new WP_Query(), query_posts()

  • query_posts: usually get the main posts in the template (main query).
  • get_posts and WP_Query: often used to get posts with conditions like getting all posts in a category…

These 3 functions all use parameters of type string or array.

In the above example, It will get a post with slug=’sample-post-slug’ and the status of the post is published. Pass the array parameter to get_posts. Then use a PHP Foreach loop to split the data and display it.

Note: for the case of getting independent posts using get_posts, if we want to use the template tag like above eg: the_title, the_content we need to get each post data into the $post variable in the iterative array returned by the get_posts function. And before you can use the template tag you will have to configure the global post using the setup_postdata function. Note that the $post variable must not be renamed with another name, the template tag will not work properly.

This does not apply to WP_Query.

Use main query loop and secondary query loops

When you get data by get_posts or WP_Query function called get second data after main page’s data. For example, the data loop in the template single.php, page.php, or category.php is the main query loop. We still often use the loop to get the data of the detail post page:

If you want to use the get_posts & WP_Query function to get the data before using the main data in the template, i.e. put the get data with these functions before the while loop above, then before starting the fetch. main data you need to restore the current template’s data by calling the wp_reset_postdata() function. Eg:

The difference between the main query loop and secondary query loops is:

The main query loop is based on the URL since the single and category URL template will output different data and be processed before loading the template. While the secondary loops are standalone queries using WP_Query.
Unlike get_posts & WP_Query, the query_posts function has the effect of modifying the parameters before getting the main data in the template. Example: edit option ‘posts_per_page’

WordPress stores the parameter string in the global $query_string variable. Another way to get the query parameter from the $wp_query variable:

Query Data

Note: numberposts is the number of posts to be retrieved including pagination, showposts: the number of posts to be retrieved does not include pagination.

Sort the returned data

Sort by Alphabet

– by title title
– the name of the author of the article (author)
– according to the post slug name
Example sorting by article title from [a-z]

Sorting forward or backward or ascending or descending can use sort_order or order, both of which play the same role.

Sort by date-time

  • Published date
  • Modified date

The following example sorts the articles by date, i.e. articles written later will be displayed at the back.

Normally WordPress will sort in reverse, new posts are displayed at the top.

This parameter is already there by default, so when you design a WordPress template, you do not need to specify this parameter. In the above example, we use the WP_Query class to get the posts data, you are familiar with the data constructor for each post the_post which takes the post ID the_ID() or get_the_ID(). Alternatively, you can use the $query->post->ID variable without having to call the_post() first.

Sort by number

  • by post/page id ID
  • sort by number of comments (comment_count)
  • sort by post/page parent id.

Example: Sort posts/pages/custom post types with decreasing number of comments.

Sort by custom field:

A custom field is a field added by the user in addition to the fields provided by the data type eg: the post data type has some fields like title, content, excerpt,… Specify the field name by the meta_key parameter, the field value is sorted by alphabet or number. The following example sorts a custom field by alpha.

If the value of the custom field is numeric, instead of using meta_value you use meta_value_num to tell WordPress to sort this field by numeric. The following example sorts posts with the custom field “field1” in descending value.

Random sort:

Randomize post/page/custom post types. Use “orderby”=>”rand”

Custom post type

Get the data of the custom post type. Specify the custom post type name in the post_type parameter.

Filter posts by taxonomy. Compare 1 or more taxonomy with relation ‘AND’, ‘OR’

And compare the taxonomy of one of the exact values ​​with the operand operator. eg: IN, NOT IN
Note: the same type of comparison but filtering custom fields with ‘meta_query’ will use a different parameter name ‘compare’.

Filter posts with custom post fields. The simple way is to compare a field’s value.

If the value of the field is not known, do not provide it. The result will list all posts with that field.

To compare multiple fields, we use the meta_query parameter.

You can also sort data with ‘meta_query’ using the compare attribute. Note: if the meta condition does not exist (compare=’NOT EXISTS’) then its value is meaningless, see also:

You want to compare a field but at the same time that data line has other fields, For example, I want to return articles with post date from date xx onwards by field value “the_date”, The following code we have meta_key and meta_query attributes can be combined.

The comparison field is the_date and the comparison type is greater than or equal to (>=). To sort the ASC form of the return result of the field “the_date”, we need to declare an additional field in the meta_key attribute like above. You can use other attributes to filter posts as you like eg: post_type.

Of course, Woocommerce Product is also a custom post type in WordPress, so these data retrieving functions can also get product data in Woocommerce. Many Woocommerce functions use data retrieval functions like get_posts or WP_Query.

Filter by tag and category

Filter posts with Tag.

Filter posts with category.

Get only post with thumbnail support

Note: Posts with featured images or thumbnails have the field attribute ‘_thumbnail_id’.

Exclude posts from the results

Get all but Exclude these posts. Pass the array of IDs of the posts you want to remove in the result.

Modify the where SQL statement

The parameters of the data-retrieving functions like get_posts, WP_Query, query_posts provide quite complete data filtering, can do everything, but if you don’t see the response enough or you have an idea to intervene. where string subject to interfering more deeply with the SQL command and you can even combine with other table’s data. Now you need the posts_where hook.

In the following example, I limit posts by author, the results only return posts by one author. We pass the restrict_manage_posts URL parameter to filter the author.

Write the where clause in the SQL statement to the $where a variable, all functions that get data in WordPress use this same filter. And any functions that get data will be modified depending on the function associated with the posts_where filter.

You can also allow the data filter function to ignore the posts_where filter by adding an additional parameter with the suppress_filters=>false attribute:

Get post results (Loop each post)

Using get_posts()

Using WP_Query()

Use query_posts



About Author

Leave A Reply