通过meta键排除Wordpress循环上的帖子

时间:2018-11-17 15:31:00

标签: php wordpress

在我的wordpress网站中,我使用了一个名为“ offline”的自定义字段/中继键。

我想从基本循环(在tag.php和category.php中)中排除所有将自定义字段/元键“ offline”设置为“ true”的帖子。有人能帮我吗?谢谢。

这是我目前的代码:

<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>

    ... Display post content

<?php endwhile; ?>
<?php endif; ?>

2 个答案:

答案 0 :(得分:0)

$args = array(
    'post_type'         => 'post',
    'post_status'       => 'publish',
    'posts_per_page'    => -1,
    'meta_query'        => array(
        array(
            'key'       => 'offline',
            'value'     => 'true',
            'compare'   => '!='
        )
    )
);

$query = new WP_Query($args);

if ($query->have_posts()) :
    while ($query->have_posts()) :
        $query->the_post();
        $post_id = get_the_ID();
    endwhile;
endif;

因此,具有meta_key = offline和meta_value = true的所有帖子都不会包含在循环中。

答案 1 :(得分:0)

排除所有带有此元复选框的帖子

'meta_query' => array(
    array(
        'key'      => 'meta-checkbox',
        'value'    => '1',
        'compare'  => '<'
    )
),

这对我有用。