[resolved] Excluding specific posts from a custom WP_Query() (3 posts)

  1. stevechatterton
    Posted 6 years ago #

    I've got a bit of a problem I'm trying to resolve on this page:

    It's a site about books, and the site's posts are exclusively split up between fiction, non-fiction & site news. I'm using a custom WP_Query function to keep the categories separate from each other. For example, this is how the fiction category looks under the hood:

    <?php $my_query1 = new WP_Query('category_name=fiction&showposts=7');
    while ($my_query1->have_posts()) : $my_query1->the_post();
    $do_not_duplicate1 = $post->ID; ?>

    Now I want to be able to specify certain posts to be excluded from front page display. I've tried modifying the parameters to something like this:

    $my_query1 = new WP_Query('category_name=fiction&showposts=7&p=-1,-3,-7');

    But the results have been disastrous. Does anyone know what I'm doing wrong? Better yet, does anyone know how to do it right? I'd really appreciate it if some kind soul could help point me in the right direction.


  2. MichaelH
    Posted 6 years ago #

    Did you try the post__not_in argument?

    See query_posts().

  3. stevechatterton
    Posted 6 years ago #

    Thanks, MichaelH.

    I changed my statement to this:


    'post__not_in' => array(1111,1115)

    $my_query1 = new WP_Query($args);
    while ($my_query1->have_posts()) : $my_query1->the_post();
    $do_not_duplicate1 = $post->ID; ?>

    And it does exactly what I need.


Topic Closed

This topic has been closed to new replies.

About this Topic