1
votes

I am trying to filter certain Categories of what is to be displayed in a shortcode in WordPress. I am using the below and it displays 2 Posts, but I want to be able to say "display latest posts with category "Apples" or latest post with category "Bananas" I just don't know how to do a filter command.

add_shortcode( 'latest_posts', 'latest_posts' );
function latest_posts( $atts ) {
ob_start();
$query = new WP_Query( array(
    'post_type' => 'post', 'posts_per_page' =>2,'order' => 'DESC'  ));
if ( $query->have_posts() ) { ?>
<?php while ($query->have_posts() ) : $query->the_post(); ?>

    <div class="news-mini">
      <p class="newsdate"><?php echo the_time(); ?></p>
      <h2 class="newshead"><?php the_title(); ?></h2>
      <a class="more-link" href="<?php the_permalink(); ?>" target="_blank">Read More &gt;&gt;</a>
      <hr>
      </div>
<?php  endwhile;
        wp_reset_postdata(); ?>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
1

1 Answers

1
votes

If you want load some category posts by category name , update WP_Query like below:

$query = new WP_Query( array('post_type' => 'post', 
    'posts_per_page' =>2,
    'order' => 'DESC',
    'tax_query' => array(
            'taxonomy' => 'NAME OF YOUR CATEGORY',
            'field'    => 'slug',
            'terms'    => 'category',
        ),
    )));