Support

Account

Home Forums General Issues Querying a Post Object field similarly to a Relationship field

Solved

Querying a Post Object field similarly to a Relationship field

  • Hello, this is probably a simple matter, but I admit I’m stumped. I’ve read up on post object, but am just not getting it.

    Starting with the relationship field, I put together a page based off the template of the example used on this site using the doctors and locations (https://www.advancedcustomfields.com/resources/querying-relationship-fields/). In particular, my code is similar to the single-location.php portion, where a list of doctors (and associated images and links) is pulled up accordingly to the matching related location.

    My question is…can this same sort of result happen if the doctors are connected to the location not by a Relationship field, but by a singular Post Object field? If all else is the same except that, will it still work? Or what else would need to change to make it work using Post Object?

    Happy to review any similar tutorials/walk-throughs, I just couldn’t find any.

  • A post object field and a relationship field are, for the most part, the same thing. They post store the post ID of the related post. The major difference between the two field types is that a post object field only stores a single value by default, which stores a single value in the database, unlike the relationship field that stores an array. If the post object field is set to allow multiple values then it functions exactly like the relationship field. Hope that answers your question.

  • Ahh, so using the doctors example above….

    If I, in the Doctors custom post type, have a Post Object field, I could then use that to connect Dr. Smith to a particular location, say Melbourne.

    And then on the Melbourne post page, I could display the title and featured image of Dr. Smith?

    I have a setup simliar to the relationship-field as shown in the tutorial above, but when I switched the custom field relationship to post object (only one connection, not multiple), the same results aren’t showing up.

  • If you’re using a post object that only allows a single value then the major difference would be in the reverse relationship query. Instead of this

    
    'meta_query' => array(
      array(
        'key' => 'location', // name of custom field
        'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
        'compare' => 'LIKE'
      )
    )
    

    You would have this

    
    'meta_query' => array(
      array(
        'key' => 'location', // name of custom field
        'value' => get_the_ID(),
        'compare' => '='
      )
    )
    

    another think that you’ll need to watch out for is that if the field type was a relationship and you change it to a post object, the values stored in the fields for posts before you made the change will not be altered. You will need to manually edit each post to make the change. The query is probably working and returning the same values because none of the values have actually changed.

  • That did it, thanks! I would not have gotten that difference on the compare value.

    It sounds like the multi-post object and relationship fields are pretty much the same, but I suppose one difference is in the UI when adding/editing a particular post.

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

The topic ‘Querying a Post Object field similarly to a Relationship field’ is closed to new replies.