Custom Post TypesPHP Development

Custom Querying Different Posts Types in WordPress theme folder

WP Blog page with arrow pointing to archives

I will suppose you know how you can display posts in blog page using the if condition by checking for posts existence. If you are unfamiliar with displaying WP post in an index.php file (commonly used as WordPress blog page), please refer to this post. In short, to display posts list inside the blog page template you use:

<?php
while(have_posts()){
the_post(); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_content(); ?>
<?php }
?>

The thing here is, this function only checks existence of any post within current page. which means if you use this function in other pages rather than index.php or archive.php, it will not return posts content as you want, instead it returns the current page title as available post as pages are also type of posts.

For this purpose, there is WP_Query(); object class which is used to tell WordPress to fetch post content using your criteria (like what type of post, how many posts, from which category …) This flexibility enhances content displays in theme templates, improving website functionality and user experience. Creating new instance of this WP_Query(); class you can pass as much arguments as you want as an associative array (you can read about Looping Through Arrays here). Here are some arguments that can be passed in WP_Query(); class

Arguments That can be passed as Associative array in WP_Query(); class.

  1. 'post_type': Specifies the post type(s) to retrieve.
  2. 'posts_per_page': Sets the number of posts to retrieve per page.
  3. 'post_status': Specifies the post status(es) to retrieve.
  4. 'orderby': Sets the parameter by which retrieved posts are ordered.
  5. 'order': Sets the order (ascending or descending) in which retrieved posts are ordered.
  6. 'category_name': Retrieves posts from a specific category by slug.
  7. 'tag': Retrieves posts with a specific tag.
  8. 'author': Retrieves posts by a specific author.
  9. 'date_query': Sets date-based queries for retrieving posts.
  10. 's': Searches for posts based on a keyword or phrase.

Using this WP_Query(); class which gives objects different functions or methods, you can create different object instance with WP_Query(); class to access it’s methods like $dog = new Animal(); then after that anywhere you use the function $dog->bark();

Creating An Object with WP_Query(); class

$args = array(
    'post_type'      => 'post', // Specify the post type to retrieve (e.g., post, page, custom post type)
    'posts_per_page' => 3,     // Set the number of posts to retrieve per page
    'orderby'        => 'date', // Set the parameter by which retrieved posts are ordered (e.g., date, title, meta_value)
);

$customPostsQuery = new WP_Query( $args );

In this example:

  • 'post_type' is set to 'post', which retrieves standard posts. You can change it to any registered post type, such as 'page' or a custom post type slug.
  • 'posts_per_page' is set to 3, meaning the query will retrieve up to 3 posts per page.
  • 'orderby' is set to 'date', which orders the retrieved posts by their publication date. You can change it to other parameters like 'title', 'meta_value', etc., depending on how you want the posts to be ordered.

Adding Custom Posts of Created Object on Diffetent Pages

After creating an object of WP_Query();, Instead of using default WP automatic query which is have_posts(); and the_post();, you have to use the methods or functions of the created object which are in the class WP_Query(); class. So, accessing them is like calling the object like $dog->bark(); we used in above example. Now access the object and call it’s method in WP_Query like this $customPostsQuery->have_posts(); or $customPostsQuery->the_post();

<?php
while($customPostsQuery->have_posts()){
$customPostsQuery->the_post(); ?>
<!-- ANY HTML CONTENT HERE WITH CSS CLASSES, STYLES LIKE: -->
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_content(); ?>
<?php }
?>

Displaying Partial Words of Posts

To limit words of the content displayed in custom posts, there is a WP function called wp_trim_words(); which accepts two arguments, the source and the number of words

Using the echo wp_trim_words(get_the_content(), 15); it will display only 15 words of the content

—- Thankyou for Reading —-

Shares:

Related Posts

Top Categories

PHP Development
22
WordPress Theme Development
21
Wordpress Development
18
WordPress JS Development
13
Show Comments (0)
Leave a Reply

Your email address will not be published. Required fields are marked *