I am trying to:
Display child posts from a parent, that is of the custom post type "service".
Link to a page where the issue can be seen:
I have two tests setup:
This one works, using the "types_child_posts('salon-menu')" function:
hidden link
This one is the version I need, which is not returning anything.
hidden link
I need the manual query version because I need to eventually indicate a custom order.
The query I have on this example, I have left that bit out, just for troubleshooting.
Here is the query:
$childargs = array(
post_type' => 'service-tab',
'order' => 'ASC',
'meta_query' => array(array('key' => '_wpcf_belongs_service_id', 'value' => get_the_ID()))
);
$child_posts = get_posts($childargs);
I expected to see:
The same set of posts that are dumped in the first example I listed:
hidden link
Instead, I got:
It is returning an empty array, both inside and outside the loop.
If you choose to look at these code examples on the server, you can get to both of these templates from these paths:
/httpdocs/wp-content/themes/FoundationPress/page-templates/page-test.php
/httpdocs/wp-content/themes/FoundationPress/page-templates/page-test-2.php
I have all plugins turned off except Types.
Any advice you can pass me would be greatly appreciated.
Thanks for your time.
Hi, the _wpcf_belongs_{parent-type-slug}_id method of establishing relationships is deprecated in the new relationships system (Types 3.0+), as well as the types_child_posts method. If your project is still using the legacy relationships, or if you migrate a legacy relationship into the new system, we have built in some backwards compatibility to help this type of custom code. If you create the relationship in the new system, however, this backwards compatibility layer will not apply. I logged into this site and it doesn't look like these post relationships were migrated from the old system, so I assume they were created in the new system. Check out this guide for some more detailed information about the new relationship features and shows examples of how to use them to query effectively in a WP_Query statement:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/
Another document that describes the new Post Relationships API methods you can use outside of WP_Query:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/
Let me know if you run into any blocking issues and I'll try to assist.
Thanks for your note, Christian.
I have plugged in some updated code, but still not getting anything to return.
Here is my updated query:
$tabquery = new WP_Query(
array(
'post_type' => 'service-tab',
'numberposts' => -1,
'toolset_relationships' => array(
'role' => 'child',
'related_to' => 74,
'relationship' => array('service', 'service-tab')
)
)
);
$child_posts = $tabquery->posts;
echo '<pre>' . var_export($child_posts, true) . '</pre>';
I just plugged in a hard coded id of the first service post (74) to return it's child posts, but it's still coming back as empty.
Can you see if I am missing anything simple?
Thank you.
I think it's here:
'relationship' => array('service', 'service-tab')
The post type slug array syntax only works for legacy or migrated-from-legacy relationships. Your relationship was created in the new system, so you must use a the relationship slug syntax instead of an array of post type slugs. Something like this:
'relationship' => 'service-service-tab'
You can find the relationship slug by going to Toolset > Relationships and editing this relationship.
Still no joy.
Still getting an empty array back.
Here is my updated version:
<?php
$tabquery = new WP_Query(
array(
'post_type' => 'service-tab',
'numberposts' => -1,
'toolset_relationships' => array(
'role' => 'child',
'related_to' => 74,
'relationship' => 'service-service-tab'
)
)
);
$child_posts = $tabquery->posts;
echo '<pre>' . var_export($child_posts, true) . '</pre>';
?>
Thanks for your time.
I'm not exactly sure why WP_Query isn't working as expected here without digging into your theme files. It could be that the WP_Query is being overridden somewhere, or it could be that the query happens before Toolset is fully initialized. Note this comment about init and priority: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#remarks
At any rate, the toolset_get_related_posts API seems to be working. I added an example in the Service Test 2 Template file.
Yes, I see that. I still need the query version so I can define some custom ordering.
I tried locally to get it to work after switching to TwentyFifteen, with the same results.
I guess I will try setting this up on a new blank local dev install of WP to try and at least first get it to work and then see if I can isolate what is breaking it on the previous dev version we both have been looking at.
Will let you know what I can come up with.
Thanks again.
Sure, let me know. You might also try putting this query in a shortcode and placing it on a custom page somewhere else on the site, so you can be sure Toolset is all set and ready to go when the code is called. If it still fails to produce results, something else is going on and I'll have to take a closer look.
Thanks Christian.
I set up a blank site locally and just used TwentyFifteen as the theme and still couldn't get the WPQuery to work.
I think I can use the toolset_get_related_posts function you setup.
Is there a way to set the orderby based on 'menu_order'?
I read in the documentation that it only accepts 'title' or a metavalue, but didn't know if there is a way I could use 'menu_order' somehow. I need to allow the client to somehow manually order the child items. I can see the 'menu_order' updating based on sorting the child items using a sorting plugin, but the actual order presented is not affected in the loop.
Here is what I have setup from your code currently:
$tbs = toolset_get_related_posts(
74, // get posts related to this one
'service-service-tab', // relationship between the posts
'parent', // get posts where parent id is 74 in given relationship
100000, 0, // pagination
array(), // How was his surname, again…?
'post_object',
'child',
'menu_order'
);
echo 'tbs: <pre>' . var_export($tbs, true) . '</pre>';
Let me know what you think.
Thanks for your continued support on this.
No, I'm not aware of a way to use the ordering option like this for menu_order. Let's try to get WP_Query working. Please add this code to your child theme:
add_action( 'init', function(){
$test = new WP_Query(
array(
'post_type' => 'service-tab',
'toolset_relationships' => array(
'role' => 'child',
'related_to' => 74,
'relationship' => 'service-service-tab'
),
)
);
$tabs = $test->posts;
error_log('test 1: ' . print_r($tabs, true));
$testRel = toolset_get_related_posts(
74,
'service-service-tab',
'parent',
1000000,
0,
array(),
'post_id',
'child'
);
error_log('test 2: ' . print_r($testRel, true));
}, 100);
This init hook should eliminate timing from the possible sources of the problem. Load any page on the site and watch the logs. Let me know what you find out.
So odd.
Now the WPQuery is working. Also working when I have it just sitting inline inside the template file.
Now that i have a working version on a blank theme using TwentyFifteen, I will slowly add in the same components, theme files, etc. as the problem site, to try to identify what is causing the blocking previously.
Will let you know what I find out.
Thanks!
Okay thanks for the update, I will stand by for more information.
Hi Christian.
It is still a mystery what happened, but the last query code suddenly started working for me. Not sure what changed if anything. The code was fine in my new blank setup. Tried pulling it over in the working project and now it's all working.
Anyway, thank you for your help on this.