Support

Account

Home Forums Add-ons Flexible Content Field Get relationship from custom post

Solved

Get relationship from custom post

  • Hi,

    I have a custom post that are actors, actresses…and then a normal post that are news related to that actors, actresses…I create a relationship field in the news but i don’t know how from the custom post actors (actors, actresses…) i can get the related news to them.

    This is my code. Do a query for getting de related field of the news and then compare to de ID of current actor and show results…It’s complicated and don’t work as expected…

    $terms1 = $post->ID;
    
    $query = new WP_Query(
        array(
            'post_type' => 'post',
            'posts_per_page' => -1,
            'post_status' => 'publish',
            'meta_query' => array(
                array(
                    'key' => 'nociticias_relacionadas',
                    'compare' => 'EXISTS'
                )
            )
        )
    );
    
    				
    			if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
    			
    			
    					
    				$relacion = get_field('nociticias_relacionadas', $relacion->ID);
    			
    					if(!empty($relacion)): 
    	
    						foreach( $relacion as $p ): 
    	 
    						 $terms2 = $p->ID;
    	 
    						endforeach;
    	 
    					endif;
    					
    			if ($terms2 == $terms1){  
    			
    			
    	 			$image = get_the_post_thumbnail( $query->ID, 'noticias' );?>
                    
    	   			<li class="noticiasr" style="display: inline;">
                    
            			<div class="parafade" style="float:left; max-width: 212px; text-align:center; font-size:12px; color:#aaaaaa; margin-right: 6px; line-height: 18px;">
          				<a href="<?php echo get_permalink(); ?>"> <?php echo $image; ?> </a>
            			<?php echo get_the_title(); ?>
              
            			</div>
          	
            		</li>
       			<?php
    			} 
    			

    What will be a best and working solution? I don’t get it…

    Thanks and regards,

  • Hi @raulbofill

    I need some clarification on what you want to achieve.

    So you have regular posts in which you have a relationship field where the user can connect actors/actresses to posts. And now you want to display the posts related to these actors/actresses when on a single actor/actress page?

  • Hi,

    Yes it’s exactly what i want and i figure out how to solve.

    I mess up with the code and i see it’s quite simple. I attach the final code in custom posts.

    Thanks and regards,

    $related_articles = get_posts(array(
        'post_type' => 'post',
    	'posts_per_page' => 5,
        'meta_query' => array(
            array(
                'key' => 'nociticias_relacionadas', // name of custom field
                'value' => '"' . get_the_ID() . '"',
                'compare' => 'LIKE'
            )
        )
    ));
    
    if( $related_articles ): 
        foreach( $related_articles as $article ): 
    
    	$imag = get_the_post_thumbnail( $article->ID, 'noticias' );?>
                    
    	<li class="noticiasr" style="display: inline;">
                    
               <div class="parafade" style="float:left; max-width: 212px; text-align:center; font-size:12px; color:#aaaaaa; margin-right: 6px; line-height: 18px;">
          	   <div class="fondo"><a href="<?php echo get_permalink($article->ID); ?>"> <?php echo $imag; ?> </a></div>
               <?php echo get_the_title($article->ID); ?>
              
               </div>
          	</li>
    endforeach;
    endif; 
  • Hi @raulbofill

    Alright great that you figured it out! It’s quite simple when you how how to do it and I believe there are tutorials for these things under documentation as well.

    Good luck with your project!

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Get relationship from custom post’ is closed to new replies.