Support

Account

Home Forums General Issues Displaying Related Posts – Relationship

Solved

Displaying Related Posts – Relationship

  • I am about 90% complete but I am struggling to display related posts that belong to a relationship using CPTs.

    I have a competition (post) that is related to a brand (brands). On the post I was able to display the related brand (by relationship) but what I also want to display is other posts that share the same brand.

    Relation

    Current code is displaying all relationships no matter the brand:

    
    $brands = get_posts(array(
    	'post_type' => 'competitions',
    	'posts_per_page'    => 3,
            'orderby' => 'date',
            'order' => 'ASC',
            'offset' => 0,
    ));
    			
    <?php if( $brands ): ?>
    	<ul>
    		<?php foreach( $brands as $brand ): ?>
    			<li>
    				<a href="<?php echo get_permalink( $brand->ID ); ?>">	<?php echo get_the_title( $brand->ID ); ?>	</a>
    			</li>
    		<?php endforeach; ?>
    	</ul>
    <?php endif; ?>

    I also tried ‘post__in’ as per https://www.advancedcustomfields.com/resources/relationship/

    $ids = get_field('related_brand', false, false);
    
    $query = new WP_Query(array(
        'post_type'         => 'competitions',
        'posts_per_page'    => 5,
        'post__in'          => $ids, // Returns Nothing
        'post_status'       => 'any',
        'orderby'           => 'post__in',
    ));

    But no results are returned. Where am I going wrong?

  • Ok, so you want to get all posts type competitions that share the same brand as the current post? If so, you would need a meta_query:

    https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

    $query = new WP_Query(array(
        'post_type'         => 'competitions',
        'posts_per_page'    => 5,
        'post_status'       => 'publish',
         'meta_key'		=> 'related_brand',
         'meta_value'	=> get_field('related_brand') // This is related brand of current post.
    ));

    See if the query returns anything back?

  • Thanks for chipping in.

    Using 'meta_value' => get_field('related_brand') throws up wpdb::prepare was called incorrectly. Unsupported value type (object).

  • No worries – what type of field is ‘related_brand’, exactly?

  • Relationship field – Everything else works as it should with the field, I am just struggling to pull all posts that share the same brand.

  • This worked.

    $competition = get_field( 'related_brand' );
    $competition_array = $competition[0];
    $competition_ID = $competition_array->ID;
    
    $posts = get_posts(array(
      'post_type'         => 'competitions',
      'posts_per_page'    => 6,
      'meta_query'        => array(
          'relation'      => 'AND',
          array(
            'key'         => 'related_brand',
            'value'   => '"' . $competition_ID . '"',
            'compare' => 'LIKE'
          )
        )
    ));
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Displaying Related Posts – Relationship’ is closed to new replies.