2

Question

How can I modify this code to not show posts from new categories that we just added.

I searched other similar answers on this site [such as: Exclude category on blog list page ] but not sure how to convert that into usable code in my project.

Background

We have different pages that will show links to recent posts. On the main blog page we need to hide links to posts from 5 different categories.

Code - page

<ul>
<?php
    if (isset($_GET['tag'])):
        $tagparam    = filter_var($_GET['tag'], FILTER_SANITIZE_STRING);
    endif;

    $postslist = get_regular_posts_query('25','',$tagparam);
    while ( $postslist->have_posts() ) {
        $postslist->the_post();
?>

Code - functions.php

// Get Non-Featured Posts using WP_QUERY
function get_regular_posts_query($numberposts,$categoryName=NULL,$tagName=NULL){

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    $args = array(
        'posts_per_page' => $numberposts,
        'post_status'    => 'publish',
        'category_name'  => $categoryName,
        'post_type'      => 'post',
        'orderby'        => 'post_date',
        'tag'            => $tagName,
        'meta_query'=>array(
            'relation'=>"OR",
            array(
                'key' => "featured_post",
                //'value' => "featured",
                'compare' => "NOT EXISTS"
            ),
            array(
                'key' => "featured_post",
                'value' => "",
                'compare' => "="
            )
        ),
        'paged' => $paged,  
    );

    $posts = new WP_Query( $args );

    if(! $posts ) {
        //throw new Exception("NoSuchPost");
        echo "No Articles Found!";
    }

    return $posts;
}

From other research that I did, i found that you have to put in the IDs of the categories. Mine are: 89, 90, 91, 92, 93.

0

The method given in the WPSE answer you linked would exclude posts from the categories listed by ID in the array when the current query is the home blog page (not an archive page, or a category page, etc.). It is also not intended to affect secondary queries on the page. Placed in the functions.php of theme, it should do just that.

modified comments and added your 5 cat IDs:

function my_exclude_category( $query ) {

    if ( $query->is_home() && $query->is_main_query() ) { 
//check it is home blog page and the query is main query, not a secondary query

        $query->set( 'category__not_in', array( 89, 90, 91, 92, 93 ) ); 
//adds this query var to home page main query

    }
}
    add_action( 'pre_get_posts', 'my_exclude_category' ); 
//this is hooked right before query

More info on Wp_Query class category args || pre_get_posts() action hook


To instead add the category__not_in parameter to the $args array of the function you included:

$args = array(
        'posts_per_page'    => $numberposts,
        'post_status'       => 'publish',
        'category__not_in'  => array( 89, 90, 91, 92, 93 ),
        'post_type'         => 'post',
        'orderby'           => 'post_date',
        ...(etc)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.