Расширенный пользовательский фильтр полей Wordpress - PullRequest
0 голосов
/ 25 апреля 2018

Может кто-нибудь объяснить, почему работает первый раздел кода, но не второй.Я пытаюсь отфильтровать содержимое на веб-сайте в зависимости от того, какие значения были выбраны в серверной части для каждого сообщения с использованием расширенных настраиваемых полей.

function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
    return $query;
}

// only modify queries for 'event' post type
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'property' ) {
    // allow the url to alter the query
    if( isset($_GET['bedrooms']) ) {
        $query->set('meta_key', 'bedrooms');
        $query->set('meta_value', $_GET['bedrooms']);
    } 
}
// return
return $query;

}

add_action('pre_get_posts', 'my_pre_get_posts');

не работает:

add_action('pre_get_posts', 'my_pre_get_posts');

function my_pre_get_posts( $query ) {
// bail early if is in admin
if( is_admin() ){
    return;
} 


// get meta query
$meta_query = $query->get('meta_query');

if( isset($_GET['bedrooms']) ){
    $meta_query[] = array(
        'key'        => 'bedrooms',
        'value'      => $_GET['bedrooms'],
        'compare'    => '=',
    );
}

// update meta query
$query->set('meta_query', $meta_query);

return;
}
...