0

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("<script>console.log('********************* is_main_query')</script>");    
     }else{
      echo("<script>console.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?

3
  • 2
    It could be the builder you're using. I would try adding the same snippet to a fresh install of WP with no other plugins and a default theme. If you then see some queries that are the main query, you'll know it's either your builder or one of the other plugins on the site you're really working on.
    – WebElaine
    May 2 at 13:49
  • 2
    You mention "Oxygen Repeater". This would be something specific to the plugin and might not be using the default query the way you expect it to. If it's not using the actual main query then is_main_query() would never be true. This is really something you need to ask the developers of Oxygen builder, since it's ultimately an issue with their plugin. May 2 at 13:59
  • the first snippet i posted actually comes from a well-regarded source and is a specifically about using Oxygen Builder, so Oxygen itself shouldn't be the issue... whether it's one of my other plugins is a different matter... i'll do some testing...
    – Dog
    May 2 at 14:05

0

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.