Support

Account

Home Forums General Issues Display posts that are related to the current post

Solved

Display posts that are related to the current post

  • Hi Elliot ๐Ÿ™‚ !

    I have a question for you.

    I have 2 custom post types :
    product and event

    Thanks to your “Post Object” field, each event can be “linked” to a product. As I’ve understood, your “Post Object” field returns an post object. On a single product page, I would like to display the events linked to the current product.

    Example :

    I have 2 events that are linked to the product “Blue product”. On the “Blue product” single post page, I would like to display the 2 events.

    I’ve tried that but it doesn’t work…

    $args = array ( 
          'post_type'       => 'event',
    		  'orderby'         => 'title',
    		  'order'           => 'asc',
          'meta_query'      => array(
        		array(
        			'key'     => 'post_object_field',
        			'value'   => get_post($post->ID),
        			'compare' => '='
        		)
        	)
        );  
        $events = get_posts($args);

    Could you please help me ?

    Thank you !

  • Hi @cedric_charles

    There are 2 possible issues with the above code:
    1. Is the key ‘post_object_field’ your field name
    2. The value should be just the post_id, not the object.

    It is most likely issue #2

    Hope that helps

    Thanks
    E

  • Hi @elliot

    1. ‘post_object_field’ was not the real field name, I give this to you because the real name was a weird french word, so I thought it was easier for you to understand this name ๐Ÿ˜‰ !

    2. You are right ! It had to be just the post_id, thanks ! But why ? the ACF field returns an object so why do I have to compare to just the object’s ID ?

    Thanks for your help ๐Ÿ™‚ !

    Cรฉdric

  • Hi @cedric_charles

    Thanks for the follow up.
    The get_field will return an object, but it only saves an ID into the DB. This is why you need to search for only the ID.

    Thanks
    E

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

The topic ‘Display posts that are related to the current post’ is closed to new replies.