Using WP Query for Custom Posts

WP Query can be used to fetch posts as per your requirement. Simple syntax for WP_Query is as below

WP_Query for Custom Posts

  <?php  $args = array(
    'post_type' => 'modernquote',
    'tag' => 'success',       
     'orderby' => 'post_date',
     'order'   => 'ASC');
   
  // Custom query.
  $query = new WP_Query( $args );
   
  // Check that we have query results.
  if ( $query->have_posts() ) {
   
      // Start looping over the query results.
      echo "<ul class='list-group '>";
      while ( $query->have_posts() ) {
          $query->the_post();
      // echo '<a href="' .get_post_permalink() .'"><li class="list-group-item">'. get_the_title() . '</li></a><br>';
      echo '<a href="' .get_post_permalink() .'" class="list-group-item">'. get_the_title() . '</a>';
      }
      echo "</ul>"; 
  }
  // Restore original post data.
  wp_reset_postdata();
  
  ?> 

WP_Query for Custom Posts with conditions

  <?php  $args = array(
    'post_type' => 'modernquote',
    'tax_query' => array(
        array (
            'taxonomy' => 'modernquotetaxonomy',
            // 'field' => 'slug',
            'terms' => 'love hindi',
        )
    ),      
     'orderby' => 'post_date',
     'order'   => 'ASC');
   
  // Custom query.
  $query = new WP_Query( $args );
   
  // Check that we have query results.
  if ( $query->have_posts() ) {
   
      // Start looping over the query results.
      echo "<ul class='list-group '>";
      while ( $query->have_posts() ) {
          $query->the_post();
      // echo '<a href="' .get_post_permalink() .'"><li class="list-group-item">'. get_the_title() . '</li></a><br>';
      echo '<a href="' .get_post_permalink() .'" class="list-group-item">'. get_the_title() . '</a>';
      }
      echo "</ul>"; 
  }
  // Restore original post data.
  wp_reset_postdata();
  
  ?> 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.