• I am trying to exclude certain posts from a “recent posts” section loaded on my homepage. I would like to exclude them by tag, but by ID or category would be alright as well (in that order of preference). I assume this is the code I should be editing:

    <?php
                $limit = get_option('posts_per_page');
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                query_posts('showposts=' . $limit . '&paged=' . $paged);
                $wp_query->is_archive = true;
                $wp_query->is_home = false;
                ?>

    but cannot figure out what to change. Link to site: https://harvardelp.wpengine.com/welcome/ where I’m hoping to filter out the posts beneath the slider (specifically, to exclude anything included in the slider). Any help would be appreciated, thanks.

Viewing 11 replies - 1 through 11 (of 11 total)
  • It is recommended to no longer use query_posts() and switch to WP_Query instead but to answer your question:

    To exclude a category using your existing code, just update the following line where 1 is the ID of the category you want to exclude.

    query_posts('showposts=' . $limit . '&paged=' . $paged . '&cat=-1');

    Thread Starter cjv17

    (@cjv17)

    Thank you! Is there a similar option for selecting based on tags?

    You should be able to include the tag__not_in parameter in your query.

    Thread Starter cjv17

    (@cjv17)

    I tried that —

    I included:

    $exclude = get_term_by('slug','ExcludeThisTag','post_tag');
    $exclusion = 'tag_not_in' => array($exclude->term_id);
    query_posts('showsposts=' . $limit . '&paged=' . $paged . $exclusion);

    Sorry, I do not have much experience with PHP. Do you know the mistake I am making here?

    I am not in a position to test it currently (I can in a little while if the issue still persists) but the first thing I’m noticing is it looks like you are missing an ampersand and an underscore.

    $exclusion = 'tag_not_in' => array($exclude->term_id);

    should be

    $exclusion = '&tag__not_in' => array($exclude->term_id);

    Thread Starter cjv17

    (@cjv17)

    Woops — I typed those here incorrectly, but it is actually correct in my code. Nonetheless, that code seems to crash the page. Not sure why, however.

    Let’s just do this the recommended way. I just tested the following tag exclusion and it is working fine for me.

    $exclude = get_term_by( 'slug', 'ExcludeThisTag', 'post_tag' );
    $newquery = new WP_Query();
    $newquery->query( array( 'tag__not_in' => $exclude->term_id ) );
    
    while ( $newquery->have_posts() ) : $newquery->the_post(); ?>
        // your loop stuff here
    <?php endwhile; ?>
    Thread Starter cjv17

    (@cjv17)

    I’ll give this a shot, thank you. I am using the Infoway theme and it was using query_posts, and I was trying to avoid changing too much, but I trust that this will work better.

    Just create a child theme and you won’t lose your changes when you update the theme.

    The post__not_in function isn’t working in 4.2.3.

    @heyjones Hey! Welcome to the forums, please start your own thread so both of your issues can be addressed individually. I would be more than happy to assist you there. Make sure you are passing an array of post id(s). The following is working perfectly fine for me on 4.2.3.

    Exclude post with id of 219

    $exclude_ids = array( '219' );
    $new_query = new WP_Query();
    $new_query->query( array( 'post__not_in' => $exclude_ids ) );
Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Exclude Posts from "Recent Posts" Section’ is closed to new replies.