Support

Account

Home Forums General Issues Get a list of posts where a custom field matches with the current ID

Solved

Get a list of posts where a custom field matches with the current ID

  • Hello,

    I need help to make a request on my wordpress, i tried all day but did’nt find any solution.

    Here’s what i’m trying to do :
    I have a custom post named ‘production’ and another one named ‘artist’.
    When i create a ‘production’ post, i associate some ‘artist’ posts to this production with a custom select field ( = ‘artistes’) (i get an array of IDs when i call the data).
    I have no problem to get the list of the artists on the single page production.
    —> My problem is that i want to do the opposite on the single page artist. So i want to get the list of the productions where this artist is associated.

    I tried something like this on my single page artist (but didn’t work) :

    // i get the ID of the current single page artist
    $idArtist = get_the_ID();

    // then i write the parameters of my request (get all the production where the artist is associated)
    $args = array(
    ‘post_type’ => ‘production’,
    ‘artistes’ => array(
    ‘post_ID’ => $idArtist
    )
    );
    $the_query = new WP_Query( $args );

    Any idea ?
    Romain

    (i’m french so sorry if it’s not very clear… Don’t hesitate to ask me for more details if you need)

  • https://www.advancedcustomfields.com/resources/query-posts-custom-fields/ see the section on querying on array based values. Also see this guide https://www.advancedcustomfields.com/resources/querying-relationship-fields/

    
    $args = array(
      ...
      'meta_query' => array(
        array(
          'key' => '',
          'value' => '"'.$idArtist.'"',
          'compare' => 'LIKE'
        )
      )
    );
    

    See https://developer.wordpress.org/reference/classes/wp_query/ for more info on properly constructing queries.

  • Thanks a lot, it works now !

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

The topic ‘Get a list of posts where a custom field matches with the current ID’ is closed to new replies.