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
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
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/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.
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']);
}
}