Site logo

Archived topic

problem functions display category

7 replies · Started by alexis on August 17, 2020

Viewing posts 1–8 of 8

Hello, I added this in the functions file :

add_action( 'pre_get_posts', function( $query ) {
    if ( $query->is_main_query() ) {
        $query->set( 'cat', -5 );
    }
} );

But it changes my back office dasboard. Articles don't appear anymore. How can I do that?

On my Blog page I want to display only certain categories

Hi there,

Where are you adding the code?

In the child theme's function.php?

Yes

I already had this function

add_filter('pre_get_posts', 'excludeCat');
function excludeCat($query) {
      if ( $query->is_home ) {
            $query->set('cat', '-5');
      }
      return $query;
};

I put them back => disabled because on this page https://ac9a-86a90d6267d0.wptiger.fr/lm-wedding-planner it removes the related post. I had added the following function also for related posts (which worked very well) :

add_filter( 'get_next_post_excluded_terms', 'tu_exclude_terms' );
add_filter( 'get_previous_post_excluded_terms', 'tu_exclude_terms' );

function tu_exclude_terms() {
    if ( has_category( 4 ) ) {
        return array( 5 );
    } elseif ( has_category( 5 ) ) {
        return array( 4 );
    }
};

In fact, I wish that on my Blog page there are only the articles of "x" category. When you click on these posts you will find related posts from the same category at the bottom.

On my Achievements page, inside a post in this category there are only related posts from the same category.

The function I set up worked fine but just disabled my dashboard display of the posts

In fact, I wish that on my Blog page there are only the articles of “x” category.

That case wouldn't it be better if you just use the category archive instead of the main posts page?

I created a page with WP Show Posts and it works. Thanks

No problem :)

This archived topic is closed to new replies.