Support

Account

Home Forums General Issues get_posts with meta_key on relationship field

Solving

get_posts with meta_key on relationship field

  • I can not make a simple loop work.
    I have a custom post (club_type) in which there is a relational type field to link another custom post (comite_type).

    This loop should show me related “club_type” custom post with a particular custom post depending on its ID.

    I get the $ IDcomite variable above, it contains the ID of the custom post comite_type that is linked in the relationship field.

    “comite_associe” is the name of relationship field. I retrieve the ID and not the article object.

    $posts = get_posts(array(
    ‘post_type’ => ‘club_type’,
    ‘post_status’ => ‘publish’,
    ‘posts_per_page’ => -1,
    ‘meta_query’ => array(
    ‘key’ => ‘comite_associe’,
    ‘value’ => $IDcomite,
    ‘compare’ => ‘LIKE’
    ),
    ‘order’ => ‘DESC’
    ));

    I tried with LIKE, ==, IN … nothing works.

    Thanks for your help
    Eric

  • I answer myself :

    $posts = get_posts(array(
       'post_type' => 'club_type',
       'post_status'      => 'publish',
       'posts_per_page'   => -1,
       'meta_query' => array(
       array(
          'key' => 'comite_associe',
          'value' => '"' . $IDcomite . '"',
          'compare' => 'LIKE'
          )
       )
    ));
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘get_posts with meta_key on relationship field’ is closed to new replies.