Изменить Wordpress запрос глобально - PullRequest
0 голосов
/ 27 февраля 2019

Я хотел бы изменить результаты сообщений и показывать только сообщения с определенным мета-значением (для всей страницы).Я не нашел ничего подходящего.

function cheese_filer() {
    $q = [
        'meta_query' => array(
            array(
                'key'     => 'cheese',
                'value'   => '3',
                'compare' => 'LIKE',
            )
        )
    ];

    return $q;
}

add_action('pre_get_posts','myf88');

function myf88($query) {
    if (  $query->is_category ) {
       $query->set('post_type', array('post','page','my_postType') );
       add_filter( 'posts_where' , 'cheese_filer' );
    }
}

1 Ответ

0 голосов
/ 27 февраля 2019

Я бы сделал это следующим образом, используя функцию WP_Query():

$args = array(
    'post_type' => array('post','page','my_postType'),
    'category_name' => 'your_category',
    'meta_query' => array(
          'key'     => 'cheese',
          'value'   => '3',
          'compare' => 'LIKE'
    )
);
$my_loop = new WP_Query($args);

А затем цикл:

if ( $my_loop->have_posts() ) : while ( $my_loop->have_posts() ) : $my_loop->the_post(); 
  [ ... ] // get the custom field contents
endwhile;  else:
  [ ... ] // nothing found message/action
endif; 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...