1

In order to have posts in ascending order in a specific category, I inserted the following string in function file:

function reverse_order($query) {
if(!is_admin() && $query->is_main_query() && ($query->is_category(314) || cat_is_ancestor_of(314,get_category_by_slug( $query->get( 'category_name' ))))) {
        $query->set('order','ASC');
    }
}
add_action('pre_get_posts', 'reverse_order');

Everything would seem to work, except that every seven minutes, in error.log is notified the following problem, of which therefore, I'd like to understand the solution:

PHP Warning:  Attempt to read property "term_id" on null in /home/*****/public_html/wp-includes/class-wp-query.php on line 3761

PHP Warning:  Attempt to read property "name" on null in /home/*****/public_html/wp-includes/class-wp-query.php on line 3763

PHP Warning:  Attempt to read property "slug" on null in /home/*****/public_html/wp-includes/class-wp-query.php on line 3765

The strings involved are consequently these:

$cat_obj = $this->get_queried_object();

        $category = array_map( 'strval', (array) $category );

        if ( in_array( (string) $cat_obj->term_id, $category, true ) ) {
            return true;
        } elseif ( in_array( $cat_obj->name, $category, true ) ) {
            return true;
        } elseif ( in_array( $cat_obj->slug, $category, true ) ) {
            return true;
        }

        return false;
    }

Thanks in advance

6
  • Hi @Sally, thank you so much for the suggestion, but, please forgive me, where in the above inserted function, I should add these lines?
    – Jpganja
    Sep 22 at 8:46
  • Lol, sorry, ignore that comment (I've deleted it, anyway) - I didn't notice its the WordPress code and not yours.. :p
    – Sally CJ
    Sep 22 at 9:05
  • However, as a workaround, I think you can avoid the issue by checking if there is a queried object, like so: in your code, change the && $query->is_main_query() to && $query->is_main_query() && $query->get_queried_object() And I'm not sure if that is a bug in WordPress core, but you could try deactivating plugins and see if the same issue persists? Are you using WordPress 5.8?
    – Sally CJ
    Sep 22 at 9:16
  • 1
    You're great @SallyCJ, it works! Thanks so much!
    – Jpganja
    Sep 23 at 8:40
  • 1
    Well, I'll check then and possibly report the problem. You're very kind indeed. Thanks so much again.
    – Jpganja
    Sep 24 at 15:09

Your Answer

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

Browse other questions tagged or ask your own question.