2

Is there a way to query all pages and order them by menu_order but ignore those pages that the default value of 0?

I was trying to do something like this:

        $the_query = array( 
            'post_type' => self::POST_TYPE, 
            'posts_per_page' => $total, 
            'product_cat' => $product_category_name, 
            'orderby' => $orderby,
            'suppress_filters' => '0' 
        );

Or do I need to create a filter to alter the WP_Query? any ideas?

cheers

2 Answers 2

3

You can try the following (untested) mini plugin:

<?php
/**
 * Plugin Name: Support for ignoring the default menu order in WP_Query
 * Description: Uses the _ignore_default_menu_order argument
 * Plugin URI:  http://wordpress.stackexchange.com/a/193291/26350
 */
add_filter( 'posts_where', function( $where, $q )
{
    global $wpdb;

    if( (bool) $q->get( '_ignore_default_menu_order' ) ) {
        $where .= "AND {$wpdb->posts}.menu_order <> 0";
    }
    return $where;

}, 10, 2 );

Then you should be able to use the new custom query argument like:

$query = new WP_Query( 
    [ 
        '_ignore_default_menu_order' => true,
    ]
);

to ignore posts with the default menu order (0).

You could also extend this to support any menu order as user input.

6
  • 1
    Thanks mate, this was a great start point to be able to finish it :) Commented Jul 2, 2015 at 12:39
  • This doesn't work because instead of removing the 'orderby menu_order' from the query, it inserts a 'where menu_order <> 0' and turns out to not display the posts intended too.
    – Vamoss
    Commented Dec 19, 2017 at 20:30
  • @CarlosOliveira The OP includes this part: "Is there a way to query all pages and order them by menu_order but ignore those pages that the default value of 0?" so I'm not sure what you mean by "removing the 'orderby menu_order' from the query". It looks like you have another understanding of the original question than I do.
    – birgire
    Commented Dec 20, 2017 at 9:44
  • Thanks for clarifying, I was influenced by my own problem, it is more related to this thread, in case you want to help: stackoverflow.com/questions/47613024/…
    – Vamoss
    Commented Dec 20, 2017 at 14:52
  • 1
    @CarlosOliveira sure, no problem, it wouldn't though be the first time that I would misunderstand questions here on this stack ;-) If you've some unwanted ordering in the WP_Query, then a plugin or a theme is what comes first to mind. So I would make sure that's not the case.
    – birgire
    Commented Dec 20, 2017 at 17:13
0

The code below worked fine for me even with the custom ordering plugins also like Intuitive CPO

add_action( 'pre_get_posts', 'custom_pre_get_posts', 20, 1);

function custom_pre_get_posts($wp_query) {

    if(isset($wp_query->query['_ignore_default_menu_order']) && $wp_query->query['_ignore_default_menu_order']) {
        $wp_query->set( 'orderby', $wp_query->query['orderby'] );
        $wp_query->set( 'order', $wp_query->query['order'] );
        unset($wp_query->query_vars['_ignore_default_menu_order']);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.