$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'); ?>meta query - Why does this incorrect pre_get_posts meta_query work for sort 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)

meta query - Why does this incorrect pre_get_posts meta_query work for sort order?

matteradmin10PV0评论

I've got a fairly complicated post type archive page that I need to order by two meta fields and the post title. To do that, I'm using pre_get_posts with a meta_query that simply tests for the existence of two meta-keys. It's entirely to allow for the complex ordering required. This wasn't working until I accidentally used the wrong syntax.

Why does this code work? Update: "Fixed" snippet to show the wrong-but-functioning code. h/t @SallyCJ. Update 2: Fixed the fixed snippet so keys are correct.

<?php
add_action( 'pre_get_posts', 'wpse_pre_get_posts' );
function wpse_pre_get_posts( $query ) {
    if( ! $query->is_main_query() ) {
        return;
    }

    if( is_post_type_archive( 'my-post-type' ) ) {

    $query->set( 'meta_query', array(
            'relation'      => 'OR',
            array( // this array shouldn't be here but is required for the ordering to work
                'meta_query_1'  => array(
                    'key' => 'cis_schools_is_model',
                    'type'  => 'numeric',
                    'compare'   => 'EXISTS',
                ),
                'meta_query_2'  => array(
                    'key' => 'cis_schools_district',
                    'compare'   => 'EXISTS',
                ),
            ),
        ));

        $query->set( 'orderby', array(
            'meta_query_1'      => 'DESC',
            'meta_query_2'   => 'ASC',
            'title'             => 'ASC',
        ));

    }
}

My expectation is that the extra array wrapping the two keyed meta queries (meta_query_1 and meta_query_2) shouldn't be necessary like this:

<?php
$query->set( 'meta_query', array(
    'relation'      => 'OR',
    'meta_query_1'  => array(
        'key' => 'meta-key-1',
        'type'  => 'numeric',
        'compare'   => 'EXISTS',
    ),
    'meta_query_2'  => array(
        'key' => 'meta-key-two',
        'compare'   => 'EXISTS',
    ),
));

However, the first snippet works and the second doesn't.

Post a comment

comment list (0)

  1. No comments so far