Forums

[resolved] change query (3 posts)

  1. jaw23
    Member
    Posted 2 years ago #

    Newbie here.

    How do I change my query so it includes only one post but excludes an array of categories?

    Here's what I have but I need to change the category from 3 to a bunch of categories (and new ones which have not been created yet) so I figured the easiest way is to just exclude the ones I don't want with an array.

    <?php $recent = new WP_Query("cat=3&showposts=1"); while($recent->have_posts()) : $recent->the_post();?>

    I found an array but it does not specify showing only one post.

    Many thanks!

  2. vtxyzzy
    Member
    Posted 2 years ago #

    For example, if you do not want categories 7,12,14,15,16 you could use

    <?php $recent = new WP_Query("cat='-7,-12,-14,-15,-16'&showposts=1"); while($recent->have_posts()) : $recent->the_post();?>

    or

    <?php
    $args = array(
      'category__not_in' => array(7,12,14,15,15),
      'showposts' => 1,
    );
    $recent = new WP_Query($args); while($recent->have_posts()) : $recent->the_post();?>
  3. jaw23
    Member
    Posted 2 years ago #

    That's it!
    Thanks so much!

Topic Closed

This topic has been closed to new replies.

About this Topic