Using WP_Query – Display Custom Posts Grid WordPress

 Using WP_Query – Display Custom Posts Grid WordPress

WP_Query is just a query function in WordPress that allows developers to write custom queries and display information and articles using different parameters.

We can use WP_Query in many cases in WordPress themes as well as when creating plugins for a WordPress site. Now we want to know how to display WordPress and custom posts

The first thing you need to know is to decide which articles you want to display, whether it is the default WordPress posts or the custom posts you have created, and also the categories or custom taxonomy.

The good thing is that you can use WP_Query to display all articles of their types or even a list of pages as well. Or what you choose only based on a specific category, you have the choice specifically according to the following explanation

Examples of using WP_Query

/**
 * Setup WP_Query  to show the ‘post’   with ‘8’ posts.
 * Output the title with an excerpt.
 */
     <?php  
    $wpb_all_query = new WP_Query(array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 8, 
        'orderby' => 'title', 
        'order' => 'ASC',     
     ));?>
    <?php if ( $wpb_all_query->have_posts() ) : ?>
    <?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
        <div class="post-grid"> 
                   <a href="<?php the_permalink(); ?>">
               <h3 class="card-title"><?php the_title(); ?> </h3></a>
            </div>
             <?php endwhile; ?>  
             <?php wp_reset_postdata(); ?>
     <?php else : ?>
    <?php endif; ?>

As you saw above, we used this parameter

‘post_type’ => ‘post’, calls post type
‘post_status’ => ‘publish’ calls post status
‘posts_per_page’ => 8, calls post count
‘orderby’ => ‘title’ , is the order of posts by title
‘order’ => “ASC” asc

View WordPress articles with their distinctive image

      <?php  
    $wpb_all_query = new WP_Query(array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 8, 
        'orderby' => 'title', 
        'order' => 'ASC',     
     ));?>
    <?php if ( $wpb_all_query->have_posts() ) : ?>
    <?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
        <div class="post-grid"> 
                   <a href="<?php the_permalink(); ?>">
                    <?php
    if ( has_post_thumbnail() ) :
    $url = wp_get_attachment_url( get_post_thumbnail_id());?>
    <img  src="<?php echo $url ?>" class="img-fluid" alt="<?php the_title();?>">
    <?php endif;?>  
               <h3 class="card-title"><?php the_title(); ?> </h3></a>
             </div>
             <?php endwhile; ?>  
             <?php wp_reset_postdata(); ?>
     <?php else : ?>
    <?php endif; ?>

View WordPress articles with their excerpts

      <?php  
    $wpb_all_query = new WP_Query(array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 8, 
        'orderby' => 'title', 
        'order' => 'ASC',     
     ));?>
    <?php if ( $wpb_all_query->have_posts() ) : ?>
    <?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
        <div class="post-grid"> 
                   <a href="<?php the_permalink(); ?>">
                    <?php
    if ( has_post_thumbnail() ) :
    $url = wp_get_attachment_url( get_post_thumbnail_id());?>
    <img  src="<?php echo $url ?>" class="img-fluid" alt="<?php the_title();?>">
    <?php endif;?>  
               <h3 class="card-title"><?php the_title(); ?> </h3></a>

               <p><?php the_excerpt(); ?></p>
            </div>
             <?php endwhile; ?>  
             <?php wp_reset_postdata(); ?>
     <?php else : ?>
    <?php endif; ?>

Show articles in WordPress from one specific category

      <?php  
    $wpb_all_query = new WP_Query(array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 8, 
        'orderby' => 'title', 
        'order' => 'ASC',
        'cat' => 'home',
     ));?>
    <?php if ( $wpb_all_query->have_posts() ) : ?>
    <?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
        <div class="post-grid"> 
                   <a href="<?php the_permalink(); ?>">
                    <?php
    if ( has_post_thumbnail() ) :
    $url = wp_get_attachment_url( get_post_thumbnail_id());?>
    <img  src="<?php echo $url ?>" class="img-fluid" alt="<?php the_title();?>">
    <?php endif;?>  
               <h3 class="card-title"><?php the_title(); ?> </h3></a>

               <p><?php the_excerpt(); ?></p>
            </div>
             <?php endwhile; ?>  
             <?php wp_reset_postdata(); ?>
     <?php else : ?>
    <?php endif; ?>

the Summary

There are several parameters that you can use to customize desired posts with WP_Query. We cover some commonly used parameters below but for a more comprehensive list, take a look at the WordPress Codex reference on parameters.

cat – filters posts by taxonomy identifier
Tag – filters posts by a specific
hashtag tax_query – filters posts by specified taxonomy parameters
s – filters posts by search keyword
author – filters posts by specific author