Support

Account

Home Forums Front-end Issues Querying relationship fields not working

Solving

Querying relationship fields not working

  • Hi, Did any one made this exemple code working ?
    https://www.advancedcustomfields.com/resources/querying-relationship-fields/

    This is not working on my side, and kind of driving me crasy.
    I have a custom post called expertises.
    In post, I have a relation field called “related_expertises”, where you can add different custom post expertises.
    When I’m in single-expertises, I want to show all the post that have added this particular expertise to the relation filed.

    $related = get_posts(array( ‘post_type’ => ‘post’, ‘meta_query’ => array( array( ‘key’ => ‘related_expertises’, ‘value’ => get_the_ID() , ‘compare’ => ‘LIKE’ )) ));

    Based on the exemple this should work but return nothing. Did I get something wrong ?
    Thanks for the help !

  • try

    
    $related = get_posts(array( 'post_type' => 'post', 'meta_query' => array( array( 'key' => 'related_expertises', 'value' => '"'.get_the_ID().'"' , 'compare' => 'LIKE' )) ));
    

    Note that I am enclosing the post ID value in “”

  • Hi John,
    Thanks for taking the time.
    I removed it because of this (last comment)
    https://stackoverflow.com/questions/42011047/wordpress-query-by-acf-relationship-field

    It’s still not working. But it should right ?
    I’m wondering if I did something backward or totally miss something here.

  • That depends on the type of relationship field. A relationship field that only allows 1 selections is stored as the post ID of that selection and then you would query for posts there the value “=” that post ID. A relationship field that allows multiple values is stored as a serialized array and requires the “” around the post ID.

    More question:

    Are you using guberbug blocks? Is the relationship field a block?

    If not the above, is the relationship field a sub field of another field; repeater, group, flexible content?

  • Yes, it’s multiple selections.
    Yes I’m using guberbug blocks, but no the relationship field is not a block, it’s a classic ACF field.
    No, it’s not a sub filed.
    Since it’s multiple, shouldn’t it be something like “IS IN” instead of “LIKE” for the compare value ?

  • IN does not work, it is stored as a serialized array. IN only works when a meta field is stored in multiple DB entries. LIKE must be used for serialized arrays.

    Are all the selected posts in the relationship published?

    If that’s not it then the only thing I can think of is that the value of get_the_ID() where it is called is not the post ID you think it is. Is this in any type of a nested post loop?

  • No, It’s not a weird loop, it’s just after the regular loop.
    I’m thinking of an other way to solve my problem.
    I can revert the system, and have the post in a relation ship field in the expertises custom post instead. Then I just need to call the get_field(‘related_post’) and i will have my array of id’s.
    It will be simpler, and maybe even more logic for the redactors.
    I’m going to grab some food then get back to it.
    I will tell you how it went. Thanks for the help, I really appreciate it.

  • If it is after the regular loop and not inside “The Loop” then get_the_ID() is likely not returning the correct ID if it is returning an ID at all. You should check this

    
    // before query
    var_dump(get_the_ID());
    
  • No the id is correct, In and after the loop the title and Id are the same with the_title(get_the_ID() and get_the_ID();

  • The only other thing that I can think or is that you have some type of filter, possible a pre_get_posts filter that is interfering with the query. Could be in a plugin or the theme.

    Have you considered creating a bidirectional relationship field?

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

You must be logged in to reply to this topic.