[Resolved] problem functions display category

Home Forums Support [Resolved] problem functions display category

Home Forums Support problem functions display category

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1406479
    alexis

    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

    #1406595
    Leo
    Staff
    Customer Support

    Hi there,

    Where are you adding the code?

    In the child theme’s function.php?

    #1406652
    alexis

    Yes

    #1406832
    Leo
    Staff
    Customer Support
    #1407199
    alexis

    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

    #1407974
    Leo
    Staff
    Customer Support

    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?

    #1408159
    alexis

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

    #1408368
    Leo
    Staff
    Customer Support

    No problem 🙂

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.