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..
Just echo the meta code, had this same problem yesterday
it will just display blank..
does it display anything when you just echo the meta code
Gareth_Gillman said
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.
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
@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!
