Tagged: post_belongs, pre_get_posts
- This topic has 3 replies, 3 voices, and was last updated 5 years, 2 months ago by
Anthony.
-
AuthorPosts
-
-
November 16, 2016 at 10:34 am #7545
AnthonyMemberI am trying to create a sidebar with its conditional being to activate only if the post_belongs has a null value. In other words show the sidebar only posts that have a ‘sponsor’ post_belong relationship.
I have started with this:
<?php $args = array( 'post_type' => 'sponsor' ,'posts_per_page' => -1 ,'post_belongs' => $post->ID ,'post_status' => 'publish' ,'suppress_filters' => false ,'exclude' => $post->ID ); $the_query = null; $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ) {I know that isn’t proper syntax but essentially just want to create a conditional wrapper that won’t initiate the sidebar if the post doesn’t have ‘sponsor’ belonging to it.
'if ( $the_query->post_belongs = null ) { -
November 16, 2016 at 11:58 am #7546
dameerMemberMaybe I’m missing the point but you should be able to find the answer here:
-
November 16, 2016 at 5:05 pm #7551
JasonKeymasterHi Anthony!
I assume that’s not working? I’d probably just use get_posts, use
'fields' => 'ids'to make the query leaner, and check to see if the result is empty. In a shortened form you probably won’t use exactly, something like this:if ( empty(get_posts($args)) ) { }Hope this helps! 🙂
-
November 17, 2016 at 7:27 pm #7558
AnthonyMemberIt got me onto a better solution.
Just trying to use the pre_get_posts within the functions.php so that the single custom post type ‘sponsor’ grabs the ‘posts’ that are attributed to that ‘sponsor.’
function be_event_query( $query ) { if ( $query->is_main_query() && !$query->is_feed() && !is_admin() && is_singular('sponsor') ) { $query->set('post_type',array('sponsor')); // I am trying to add the post_belong or the post_has here!! $query->set( 'meta_query', $meta_query ); } elseif ( $query->is_main_query() && !$query->is_feed() && !is_admin() ) { $meta_query = array( array( 'key' => 'post_clearance', 'value' => 'true', 'compare' => 'IN' ) ); } } add_action( 'pre_get_posts', 'be_event_query' );
-
-
AuthorPosts
- The topic ‘Conditional if post_belongs = false’ is closed to new replies.