Support

Account

Home Forums General Issues Trouble with relationship query

Solved

Trouble with relationship query

    • spigot

    • November 1, 2013 at 2:02 am

    Hi, I’m trying to query a relationship between two CPT’s as a pretty much spot on example of this tutorial: http://www.advancedcustomfields.com/resources/tutorials/querying-relationship-fields/, but it’s not working out quite right…

    General parameters:

    • Two CPT’s: Artworks and Artists
    • Artwork CPT includes relationship field for artists
    • On the Artist single post template I’m trying to display all artwork associated with the artist
    • Template Code: https://gist.github.com/spigotdesign/7251125

    The issue I’m having… For most artists, nothing appears. There are a few, however, that show a single piece of art, even though multiple artworks are associated.

    When testing, if I removed the meta_query portion of the query only 5 posts appear.

    Can anyone tell me what I’m doing wrong here? The code seems pretty spot on to the tutorial…

    Thanks, great plugin – this is my first time using it and it’s quickly proving to be a tool I’ll use again and again.

    ~ Bryan

    • Elliot

    • November 3, 2013 at 6:51 pm

    Hi @spigot

    Thanks for all the info. Very clear!

    As for the code. You will need to add in the ‘posts_per_page’ arg and set it to -1.

    This should fit the 5 post limit issue.

    As for the code, it looks great.

    If you turn the get_posts into a WP_Query, you can then debug the actual SQL. This will help to spot any issues in the SQL.

    You can do this like so:

    
    	<?php $artworks = new WP_Query(array(
    'post_type' => 'artworks',
    'meta_query' => array(
    array(
    'key' => 'artist', // name of custom field
    'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
    'compare' => 'LIKE'
    )
    )
    ));
    
    print_r($artworks);
    die;
    

    Hope that helps

    • spigot

    • November 5, 2013 at 5:16 am

    Hi @elliot

    I was able to get the query to work by removing the quotes from the value param as such:

    'value' => get_the_ID(), // dropped the quotes around the value since its a number

    Hopefully this doesn’t cause other issues, but I’m glad it’s working. Any thoughts on why it’s different from your code? Perhaps you can explain further why the quotes were there in the first place. Have you seen issues arise with false positives?

    • Elliot

    • November 5, 2013 at 3:25 pm

    Hi @spigot

    Interesting…

    The relationship field should be saving all values as strings into the array. If they are not, then there is a current bug which needs to be fixed.

    I’ll take a look and get back to you

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.