How to build a WP query string to pull posts based on a Toolset relationship

Hello,

I am trying to create a Looper Provider using a query based on a Toolset Relationship. I used Toolset to build the CPT and custom fields. I now want to move away from the Toolset Layout and use Cornerstone Layouts. I built similar using ACF and CPT UI like in thread here:


However I am having trouble building a similar query to work with Toolset.

Any help would be greatly appreciated.

This is the query I am trying to run:
post_type=concord-albums&meta_key=artist&orderby=DATE&order=DESC&meta_query%5B0%5D%5Bkey%5D=artist&meta_query%5B0%5D%5Bvalue%5D={{dc:post:id}}&meta_query%5B0%5D%5Bcompare%5D=%3D%3D&meta_query%5B0%5D%5Btype%5D=NUMERIC

The CPT for the albums is:
concord-albums
The CPT for the Artist is:
artist

The Toolset relationship slug is:
artist_concord-albums

Hey @wowflak,

Regretfully, we are not familiar with Toolset and since it is a 3rd party plugin, we also couldn’t offer support for it.

Remember though that the Query String is just based on WP Query. Try reaching out to Toolset’s support if they can build you the WP Query or at-least guide you through Toolset’s WP Query setup.

Thank you for understanding.

Thanks, I am working with them to try and sort it out. I may need to come back here after some more clarification.

Thanks

Hi @wowflak,

You are most welcome.

This is the query they came up with which does not work.
SELECT wp_posts.* FROM wp_posts JOIN wp_toolset_associations AS wp_toolset_associations_1 ON ( wp_posts.ID = wp_toolset_associations_1.child_id AND wp_toolset_associations_1.relationship_id = 1 AND wp_toolset_associations_1.parent_id = 13738 ) WHERE 1=1 AND ((wp_posts.post_type = ‘concord-albums’ AND (wp_posts.post_status = ‘publish’ OR wp_posts.post_status = ‘private’))) AND wp_toolset_associations_1.parent_id = {{dc:post:id}} ORDER BY wp_posts.post_date DESC

Hey @wowflak,

That looks like a MySQL query and not a WP Query. If creating a WP Query isn’t possible, it’s not possible to create a Query String and therefore, a highly customized looper must be created. Regretfully, we cannot create that as part of theme support as that would require custom development. If you’re interested to let our Elite team do the work for you, please check out the service here: https://elite.theme.co/

Thank you for understanding.

Would the One service build out this?

Hey @wowflak,

Regretfully, no. This would require our Elite team to build as this is quite complex.

Thanks.

okay well I was able to get some help from Toolset and the array looks like this:

array(
    'post_type' => 'concord-albums',
    'posts_per_page' => -1,
    'toolset_relationships' => array(
        array(
            'role' => 'child',
            'related_to' => {{dc:post:id}},
            'relationship' => array( 'artist', 'concord-albums' )
        ),
          
    ),

)

However when I try and make a string I get this error:
Fatal error: Uncaught TypeError: http_build_query(): Argument #1 ($data) must be of type array, string given in /home/user/scripts/code.php:13
Stack trace:
#0 /home/user/scripts/code.php(13): http_build_query(‘array(\r\n …’, ‘’, NULL, 1)
#1 {main}
thrown in /home/user/scripts/code.php on line 13

Hi @wowflak,

It might be due to the {{dc:post:id}} added to the realted_to attribute without any quotes. I would suggest you try removing the Dynamic Content and use the blank quote like the following and after generating the query just add the Dynamic Content into the Query String.

$arr=array(
    'post_type' => 'concord-albums',
    'posts_per_page' => -1,
    'toolset_relationships' => array(
        array(
            'role' => 'child',
            'related_to' => '',
            'relationship' => array( 'artist', 'concord-albums' )
        ),
        
    ),
);

echo http_build_query($arr);

The final Query String will look like the following.

post_type=concord-albums&posts_per_page=-1&toolset_relationships%5B0%5D%5Brole%5D=child&toolset_relationships%5B0%5D%5Brelated_to%5D={{dc:post:id}}&toolset_relationships%5B0%5D%5Brelationship%5D%5B0%5D=artist&toolset_relationships%5B0%5D%5Brelationship%5D%5B1%5D=concord-albums

Please note the above query string is just converted from the above query and if that does not work, you need to ask the Toolset again or avail Elite service.

Thanks

The string worked, thank you so much!

Hi @wowflak,

Glad that we are able to help you.

Thanks

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.