Support

Account

Home Forums General Issues Advanced custom fields – relationships logic reversed

Helping

Advanced custom fields – relationships logic reversed

  • On our site we currently have ‘work’ posts that we create and then associate with an author and genre post type.

    The overall goal is: When we view an author or genre post we want to list all the work posts that we have associated/related with that certain author/genre.

    We are using the following code which seems to have us half way…

    <?php $args = array(
    ‘numberposts’ => -1,
    ‘post_type’ => ‘post’,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘related_posts’,
    ‘value’ => $post->id
    )
    )
    );
    $posts_array = get_posts( $args );
    var_dump($posts_array);
    if( $posts_array ) {
    echo ‘

    ‘;
    }
    ?>
    However the ‘value’ field in the array doesn’t work. It technically should be passing the id of the current post (author or genre) and selecting the related content. When we remove this from the array it does bring all the posts in whether they are related or not.

    In summary, we think that the ‘value’ problem may be the key in resolving the issue as that is what should be filtering the posts.

    Thanks in advance

  • Hi @wlodek

    Because the relationship field saves multiple values as a serialized array, you need to take this into account in your WP_Query args.

    To understand the problem, please read this in depth guide for this exact issue:
    http://www.advancedcustomfields.com/resources/tutorials/querying-relationship-fields/

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

The topic ‘Advanced custom fields – relationships logic reversed’ is closed to new replies.