• Resolved reedenj

    (@reedenj)


    Hi,

    I have extended the default ‘post’ type by a field ‘linked_cars’, which is a relationship field to my ‘cars’ custom post type and ‘related_cars’ field there.

    I am now wondering, can I list all posts of ‘post’ type that contain the same value for ‘linked_cars’ field?

    So, if a current post has set ‘linked_cars’ field to van and sedan, it will list all posts that also have ‘linked_cars’ set to either van or sedan.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support pdclark

    (@pdclark)

    List posts with the same values in linked_cars relationship field:

    [pods name="post" where="linked_cars.ID"]
    <a id="linked-car-{@ID}" href="{@permalink}">{@post_title}</a>
    [/pods]

    Hide the current post from the list:

    [pods]<style>#linked-car-{@ID} { display: none; }</style>[/pods]
    Thread Starter reedenj

    (@reedenj)

    Thanks for the reply. I am unable to get it to work. It still lists all posts, and not only the ones with the same values.

    The hide works.

    Plugin Support pdclark

    (@pdclark)

    Add this shortcode, [linked_cars], with a Code Snippet plugin, a new plugin file, or theme’s functions.php:

    <?php
    add_shortcode(
    'linked_cars',
    function( $atts, $content, $tagname ) {
    ob_start();

    foreach(
    (array) get_posts(
    [
    'fields' => 'ids',
    'posts_per_page' => 100,
    'post_status' => 'publish',
    'meta_query' => [
    [
    'key' => 'linked_cars',
    'compare' => 'IN',
    'value' => get_post_meta( get_the_ID(), 'linked_cars', false ),
    ]
    ],
    ]
    ) as $post_id
    ) {
    if ( $post_id !== get_the_ID() ) {
    echo pods( 'post', $post_id )->template( null, $content );
    }
    }
    return ob_get_clean();
    }
    );

    …it will output up to 100 posts linked to the same values in the linked_cars field, using the Pods template defined inline within the shortcode:

    <ul>
    [linked_cars]
    <li><a href="{@permalink}">{@post_title}</a></li>
    [/linked_cars]
    </ul>
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Use shortcode for related stuff’ is closed to new replies.