1179 posts
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Sold between 10 000 and 50 000 dollars
  • Bought between 10 and 49 items
  • Referred between 200 and 499 users
  • Has been a member for 6-7 years
mabuc says

Hello,

I have a CPT “gallery” and I have a custom field “exclude” and I want to exclude a post from the loop and when exclude field is check the specific post should be excluded in the list.. here’s my code..

$exclude_post = get_post_meta($id, "exclude", true);
 $args = array( 'post_type' => 'gallery', 'post__not_in' => array($exclude_post), 'paged' => $paged );
 $wp_query = new WP_Query( $args );

$exclude_post wont get the value as it is outside the loop.. can anyone help me here..

thank you in advance..

506 posts
  • Bought between 10 and 49 items
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 1 and 9 users
  • United Kingdom
Gareth_Gillman says

Just echo the meta code, had this same problem yesterday

1179 posts
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Sold between 10 000 and 50 000 dollars
  • Bought between 10 and 49 items
  • Referred between 200 and 499 users
  • Has been a member for 6-7 years
mabuc says

it will just display blank..

506 posts
  • Bought between 10 and 49 items
  • Envato Studio (Microlancer) Beta Tester
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 1 and 9 users
  • United Kingdom
Gareth_Gillman says

does it display anything when you just echo the meta code

1081 posts
  • Forum Superstar
  • Most Wanted Bounty Winner
  • Envato Studio (Microlancer) Beta Tester
  • Author had a Free File of the Month
  • Bought between 50 and 99 items
  • Referred between 50 and 99 users
  • Europe
  • Has been a member for 3-4 years
  • Exclusive Author
+1 more
Smartik says

does it display anything when you just echo the meta code

No, and it will not show , because he tried to get the meta from a post outside of the “loop”.

@mabuc, get this variable inside of loop and display the post ONLY if this is not set.
$args = array( 'post_type' => 'gallery', 'paged' => $paged );
$wp_query = new WP_Query( $args );

if ($wp_query->have_posts()) : 
    while ($wp_query->have_posts()) : 
        $wp_query->the_post();    
                $exclude_post = get_post_meta($id, "exclude", true);
                if(!isset($exclude_post)) {
                          //your post structure here
                }
       endwhile; 
endif;

I’m not sure if it works but it should.

24 posts
  • Exclusive Author
  • Has been a member for 1-2 years
hexahedron says

How are you handling the checkbox on “exclude” meta key? Is the key deleted upon post save if its unchecked?

$args = array(
        'post_type'  => 'gallery',
        'paged'      => $paged,
        'meta_query' => array(
                array(
                        'key'       => 'exclude',
                        'compare'   => 'EXISTS' // "EXISTS" works with >= 3.5
                    )
            )
    );

$wp_query = new WP_query( $args );

OR

$args = array(
        'post_type'  => 'gallery',
        'paged'      => $paged,
        'meta_query' => array(
                array(
                        'key'       => 'exclude',
                        'value'     => 'true',
                        'compare'   => '!='
                    )
            )
    );

$wp_query = new WP_query( $args );

For your reference: .../wp-includes/meta.php – WP_Meta_Query

1179 posts
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Sold between 10 000 and 50 000 dollars
  • Bought between 10 and 49 items
  • Referred between 200 and 499 users
  • Has been a member for 6-7 years
mabuc says

@Smartik, actually I already done that but it’s not working if your using a pagination..

@hexahedron, thanks a lot mate… this works fine in a pagination instead of using “value=’true’” it’s value=’on’..

thanks a lot!

by
by
by
by
by
by