List posts with common custom field value

Hi I’m trying to create a list of posts that have a common custom field value that are the same as the custom field value for the post they are displayed on.
I’ve found a few examples where the value is specific but I’m after an example where the value is common.
So it might be something like
Find the value for the common field for this post and then display a list of posts (with links to the post) that have custom fields that have the same value as this post. But don’t include this post (the post they are displayed on) in the list.
Thanks.

Related posts

Leave a Reply

1 comment

  1. Retrieving and Listing posts based on current posts metadata

    Assuming that the custom field is saved as postmeta:

    global $post; // current post displayed
    
    /* grab the value of the custom field for the current post */
    $custom_field_value = get_post_meta( $post->ID, 'custom-field-name', true );
    
    /* fetch all posts with the same value for the custom field */
    $args = array(
        'post_per_page' => -1,
        'meta_key' => 'custom-field-name',
        'meta_value' => $custom_field_value,
        'post__not_in' => array( $post->ID )
    );
    $related_posts = new WP_Query( $args );
    
    /* loop through related posts */
    while ( $related_posts->have_posts() ) : $related_posts->the_post();
        echo '<li>' . get_the_title() . '</li>';
        /* or do something else */
    endwhile;
    
    wp_reset_postdata();
    

    Extending the above by a second metadataset and linking to posts

    As per your latest comment below, you want to limit the amount of posts shown by another custom field value, or metadataset respectively. Further you want the post list to link to the posts.

    1. As for the linking, see the get_permalink() function.

    2. I haven’t quite understood, whether the 'other-meta-key' is supposed to be set to 'yes' on the current post or on the retrieved posts. In the first case, you can use the conditional from your comment and wrap everything in it (apart form the first line, global $post;), because everything else would be redundant.

    3. If 'yes' is supposed to be a meta value of the retrieved posts, your self-suggested approach to tackle the second meta value by adding a conditional into the loop would work as well indeed, but is not the most efficient solution. That way, you’d retrieve more posts than you need and iterate over more than you want to display. It is more efficient to incorporate this condition into the query as well and grab only the posts you want from the database. Whether you’d actually notice a difference in performance depends on the amount of posts we’re dealing with.

    Anyhow, here we go:

    global $post; // current post displayed
    /* grab the value of the custom field for the current post */
    $custom_field_value = get_post_meta( $post->ID, 'custom-field-name', true );
    /* fetch all posts with the same value for the custom field
     * and 'yes' for the second field */
    $args = array(
        'post_per_page' => -1,
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'custom-field-name',
                'value' => $custom_field_value
            ),
            array(
                'key' => 'other-meta-key',
                'value' => 'yes'
            )
        ),
        'post__not_in' => array( $post->ID )
    );
    $related_posts = new WP_Query( $args );
    
    echo '<ul>';
    /* loop through related posts,
     * note that within the loop, the $post object is not the same as above */
    while ( $related_posts->have_posts() ) : $related_posts->the_post();
        echo '<li><a href="' . get_permalink( $post->ID ) . '">' .
                 get_the_title() .
             '</a></li>';
    endwhile;
    echo '</ul>';
    wp_reset_postdata();