Giuseppe
Giuseppe

Reputation: 27

Exclude post on Wordpress loop through meta-key

In my wordpress site, I use a custom field/meta-key called "offline".

I would like to exclude from the basic loop (in tag.php and category.php) all the posts that have the custom field/meta-key "offline" set to "true". Can someone help me? Thank you.

This is my code currently:

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

    ... Display post content

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

Upvotes: 1

Views: 771

Answers (2)

Rasel Ahmed
Rasel Ahmed

Reputation: 11

Exclude all the posts that have this meta-checkbox

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

This worked for me.

Upvotes: 0

Sunil Sharma
Sunil Sharma

Reputation: 174

$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;

So all posts with meta_key=offline and meta_value=true is not included in loop.

Upvotes: 2

Related Questions