1179 posts
  • Has referred 200+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+2 more
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..

748 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
+3 more
Gareth_Gillman says

Just echo the meta code, had this same problem yesterday

1179 posts
  • Has referred 200+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+2 more
mabuc says

it will just display blank..

748 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
+3 more
Gareth_Gillman says

does it display anything when you just echo the meta code

1243 posts
  • Provided great contribution to our forums
  • Has been part of the Envato Community for over 4 years
  • Has sold $40,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+6 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
  • Has been part of the Envato Community for over 2 years
  • Sells items exclusively on Envato Market
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
  • Has referred 200+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+2 more
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