how-to-create-wordpress-theme

How to Create WordPress Custom Theme – 7 – Writing a custom query to fetch posts

Till now we use all WordPress functions to get the data like the_title, the_content etc. But what about the situation when we need to fetch the custom data or when we need to get custom post type data? At this stage we need to work with WP_Query class.

This is exactly similar what we did in custom plugin development. so Check this post to know about WP_Query class and how it uses the loop to get the data.

At this stage we need to know some WordPress functions.

WP_Query()The WordPress Query class.
wp_trim_words()Trims text to a certain number of words.
wp_reset_postdata()After looping through a separate query, this function restores the $post global to the current post in the main query.
get_post_type()Retrieves the post type of the current post or of a given post.