Support

Account

Home Forums General Issues Query posts by ACF stored in ACF related post

Helping

Query posts by ACF stored in ACF related post

  • Hey all,

    I was wondering. Can I query posts based on values stored in related posts? When I render the post itself I simply use something like this:

    $relatedPostID = get_field("location", get_the_ID());
    get_field( 'location_code', $relatedPostID )

    But how would I do this before the query? So what I want is:
    – Get posts with filled relationship fields
    – Get the value of that field inside the related post
    – Sort it alphabetical

    Any help would be appreciated.

  • Ah, found the answer.
    Solution: Run a nested query. First one returns all the related posts, already sorted. Collects them in an array and for the second query you can use ‘post__in’ => $idArray, ‘orderby’ => ‘post__in’ in the $args.

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

The topic ‘Query posts by ACF stored in ACF related post’ is closed to new replies.