skip to Main Content

This is my first question on stackoverflow.

I have problem with sorting posts. I write small plugin to sort posts alphabetically. It’s work ok.
There is a code:

function wpb_custom_query( $query ) {
    if( $query->is_main_query() AND !($query->is_home()) ) {
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'wpb_custom_query' );

and Query Monitor see query as:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
LEFT JOIN wp_term_relationships
ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE 1=1
AND ( wp_term_relationships.term_taxonomy_id IN (133,134,135,136,137,161) )
AND ((wp_posts.post_type = 'post'
AND (wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'private')))
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_title ASC
LIMIT 0, 70

But now I need change order posts by current category when user is.
I try it change it by adding "if" conditions when I check what category is currently loaded and get category name, next check if category name is euqal "NEWSY".
Code after modify:

function wpb_custom_query( $query ) {
    if( $query->is_main_query() AND !($query->is_home()) ) {

        $current_category = get_queried_object();

        if($current_category->name == 'NEWSY') 
        { 
            $query->set( 'orderby', 'date' );
            $query->set( 'order', 'DESC' );
        }
        else
        {
            $query->set( 'orderby', 'title' );
            $query->set( 'order', 'ASC' );
        }
    }
}
add_action( 'pre_get_posts', 'wpb_custom_query' );

and Query Monitor see query as:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
LEFT JOIN wp_term_relationships
ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE 1=1
AND ( wp_term_relationships.term_taxonomy_id IN (133) )
AND ((wp_posts.post_type = 'post'
AND (wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'private')))
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_title ASC
LIMIT 0, 70

It works when i go to categories named "NEWSY" but problem is in parent category (133) where all posts dissapears. I don’t have posts adding to parent category, all posts is assigned to child categories. It depenced by another plugin when i set permissions by category for users.
In Query Monitor I see that in query condition "wp_term_relationships.term_taxonomy_id" has only a parent category (133) and child categories dissapear.
I don’t know why it remove all chils categories from query.

I need help to fix this problem to see posts in parent categories back and still change sort order by category name.
I will be grateful for your help.

2

Answers


  1. I am not really sure what is wrong with your code but I suspect get_queried_object() is causing the issue somehow.

    However, you can write your code like this in a clean way.

    function wpb_custom_query( $query ) {
        if ( ! is_admin() && $query->is_main_query() && is_tax( 'category' ) ) {
            if ( is_tax( 'category', 'NEWSY' ) ) {
                $query->set( 'orderby', 'date' );
                $query->set( 'order', 'DESC' );
            } else {
                $query->set( 'orderby', 'title' );
                $query->set( 'order', 'ASC' );
            }
        }
    }
    add_action( 'pre_get_posts', 'wpb_custom_query' );
    

    Code Explanation

    1. First of all I am checking to stop this customization for the backend, for this I am adding a check ! is_admin()
    2. then I am checking if it is the main query, using $query->is_main_query()
    3. Then I am checking if we are on category pages or not, for this, I am using is_tax function and passing category as the taxonomy name.
    4. if all the conditions passed then I am again using is_tax but this time I am passing TERM value as well using is_tax( 'category', 'NEWSY' ), I am using Term name in the example, but I will suggest you use slug or ID. This line will let us know if we’re on NEWSY category page or not.
    5. the based on category page check I am writing an if and else statement and setting orderby and order args.

    Note: if you are using any custom taxonomy then you’ll have to change category to your custom taxonomy name and use term/category slug instead of term/category name.

    Login or Signup to reply.
  2. I’m late on this, but I’ve had the same issue and I’ve been able to resolve it using $query->set( 'suppress_filters', true );

    So your code would be:

    function wpb_custom_query( $query ) {
        if( $query->is_main_query() AND !($query->is_home()) ) {
            $query->set( 'orderby', 'title' );
            $query->set( 'order', 'ASC' );
            $query->set( 'suppress_filters', true );
        }
    }
    add_action( 'pre_get_posts', 'wpb_custom_query' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search