Support

Account

Home Forums ACF PRO Query for post object

Solved

Query for post object

  • Hey guys,

    I’ve already read so many threads about that topic and I don’t get why my code won’t work. Now I’m more at a point that I messed around that much that I don’t know anything no more.

    My setup is quite simple:

    • custom post type: releases
    • custom post type: artists

    In releases there’s a post object named “associated_artist” with a single select fpr picking the right artist.

    Of course my goal is to make them relate. I’m working on the single artist page and want to display all releases of the artist. (album name, release date, artwork). This is what I have a the moment:

    $args = array(
    'post_type'  => 'releases_post_type',
    'posts_per_page'  => -1,
    'meta_query'	=> array(
    array(
        'key' => 'associated_artist',
        //'value' => '"' . get_the_ID() . '"', // won't return anything
        //'value' => $artist_name, // the variable for displaying the name in frontend, doesnt work
        //'value' => 'Artist name', // won't work either. I'm not sure why  
       'compare' => 'LIKE'
        )
      )
    );
    var_dump(get_the_ID()); // returns different int-ID's for each artist 
    var_dump($artist_name);
    
    // query
    $releases = new WP_Query( $args );
    
    // loop
    while( $releases->have_posts() )
    {
    $releases->the_post();
    the_title();
    // ...
    }?>

    If I just leave the ‘value’ in $args blank, it returns all releases of all artists. That’s cool, but not exactly what I want.

    Looking forward to some helpful replies. πŸ™‚
    Best,
    RenΓ©

  • this should work

    
    'meta_query'	=> array(
    array(
        'key' => 'associated_artist',
        'value' => get_the_ID(), 
       'compare' => '='
        )
      )
    

    A post object field that only allows 1 selection does not store an array like a relationship does. It will only store and array if you allow multiple selections. Soe the quotes around the ID and the LIKE comparison is not required.

  • Thanks a lot! Now I get it.
    I made it work before by:$artist_id = get_the_id(); and using the variable instead. Couldn’t see why this should work and using the function itself won’t. πŸ˜€

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

You must be logged in to reply to this topic.