is_main_query() always returning false

I've built a Wordpress site using Oxygen builder. I'm using the 'Post Types Order' plugin to control the post order.

I have an Archive Page for a custom Post Type.

For the Loop/Oxygen Repeater I'm using the Default WP Query. My Archive page correctly shows the list of posts according to taxonomy.

For this custom post type I want to order it Alphabetically. I added the following snippet :

add_action( 'pre_get_posts', 'lit_category_filter' );

function lit_category_filter($query) {
    if ( ! is_admin()  $query-is_main_query() ) {
         if( $query-is_category('product')) {
             $query-set( 'order', 'Desc' );
         }
    }
}

add_action( 'pre_get_posts', 'lit_category_filter' );

Unfortunately this doesn't do anything.

I did a test with this hacky script :

function custom_query( $query ) {
     if( $query-is_main_query()  ) {
      echo(scriptconsole.log('********************* is_main_query')/script);    
     }else{
      echo(scriptconsole.log('is not  main query')/script);
     }
}
 add_action( 'pre_get_posts', 'custom_query' );

And on every page $query-is_main_query() returns false. So it looks my original script isn't working because it never identifies queries as being the main_query.

As I mentioned, all the pages are working correctly and displaying the correct contents, I'm just unable to add filters to the Default WP Query.

Any idea what I might be doing wrong here?

Topic functions Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.