Viewing 3 reply threads
  • Author
    Posts
    • #7545
      Anthony
      Member

      I 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 ) {

    • #7546
      dameer
      Member

      Maybe I’m missing the point but you should be able to find the answer here:

      Post to Post Relationships

    • #7551
      Jason
      Keymaster

      Hi 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! 🙂

    • #7558
      Anthony
      Member

      It 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' );
      
Viewing 3 reply threads
  • The topic ‘Conditional if post_belongs = false’ is closed to new replies.