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..

898 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
+4 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..

898 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
+4 more
Gareth_Gillman says

does it display anything when you just echo the meta code

1286 posts
  • 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
  • Has collected 50+ items on Envato Market
+5 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!

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by