$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - IgnoreSkip default value on orderby menu_order?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - IgnoreSkip default value on orderby menu_order?

matteradmin8PV0评论

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

Share Improve this question asked Jul 2, 2015 at 8:57 Jorge Y. C. RodriguezJorge Y. C. Rodriguez 1722 silver badges12 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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/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']);
    }
}
Post a comment

comment list (0)

  1. No comments so far