$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 - Pre get posts sort by meta key returns no results if meta key does not exist|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 - Pre get posts sort by meta key returns no results if meta key does not exist

matteradmin10PV0评论

I am able to sort my posts by a custom field ('tour_length_days'). However if this field does not exist on an existing post then it will return no results. I was hoping there was a way to conditionally check to see if that meta key has a value before adjusting the query?

Here is my code:

function apply_destinations_query_filter ($query)
{
    if (is_admin()) {
        return $query;
    }

    if (is_tax('destination') && $query->is_main_query()) {

        $query->set('meta_key', 'tour_length_days');       
        $query->set('orderby', array('meta_value' => 'ASC', 'date' => 'DESC'));
    }

    return $query;
}
add_action('pre_get_posts', 'apply_destinations_query_filter');

I am able to sort my posts by a custom field ('tour_length_days'). However if this field does not exist on an existing post then it will return no results. I was hoping there was a way to conditionally check to see if that meta key has a value before adjusting the query?

Here is my code:

function apply_destinations_query_filter ($query)
{
    if (is_admin()) {
        return $query;
    }

    if (is_tax('destination') && $query->is_main_query()) {

        $query->set('meta_key', 'tour_length_days');       
        $query->set('orderby', array('meta_value' => 'ASC', 'date' => 'DESC'));
    }

    return $query;
}
add_action('pre_get_posts', 'apply_destinations_query_filter');
Share Improve this question asked Jan 17, 2019 at 2:46 rhysclayrhysclay 3704 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Please use 'meta_value' => 'meta_value_num' instead of 'AC'. You can also take help from documentation to see which parameters you can pass for order attribute in the query => https://codex.wordpress/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

Post a comment

comment list (0)

  1. No comments so far