[Resolved] New WP_Query ‘toolset_relationships’ argument is not returning results
This thread is resolved. Here is a description of the problem and solution.
Problem:
Queries generated using WP_Query can include a 'toolset_relationships' argument to filter by post relationships, but on the client site it is not returning the expected posts.
Solution:
This was caused by hard-coding of the wp- table prefix in some parts of the new relationships API, and this is fixed in Types 3.0.2
Relevant Documentation:
More details on how to use WP_Query argument for querying by related posts can be found at Types Fields API page
Find more details on WP_query and how it is used in WordPress.
This support ticket is created 6 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Pretty simple what I'm trying to achieve right now. I have a legacy relationship with post types "cabin" (parent) and "climma-kit" (child). These have now been migrated and allocated a relationship slug of "cabin_climma-kit".
This appears to be working absolutely fine in /wp-admin and I can see the relationships working there. However, when using WP_Query, it always returns 0 results.
I've also tried nesting and not nesting the arrays, but elsewhere in my code I need it to work with a single nested array, and I believe this should work.
The slug must be used for new relationships, while the old uses the array.
I tested this with a migrated relationship where we have a child and parent post.
1. Created a Post relationship I legacy mode:
Toolset Parent Post type can have many Toolset Child Post types
2. Create a few posts and add a few childs
3. Install 3.0 and migrate
4. This creates a new relationship with the Toolset Parent (the "one" end of the relationship) on the left and the Toolset Child (the "many" end of the relationship) on the right.
5. Create a custom code that gets all child posts of a given parent.
Keep in mind your parent (one) is left, the child (many) is right.
The code:
I get the correct data (an array with 2 Post objects as I related 2 posts).
If this does not work, can you outline where exactly you use the code and what you do with the output, as well as make sure the parent/child position in the code is respected - it seems correct to me, but that is the only I can think of that produces an empty array - if you pass the wrong parent post ID, or if you pass the wrong relationship positions.
Thanks for your response, just before I get into it though, can I clarify:
For legacy relationships, even if they are migrated, do you always need to use the array format for the relationship, i.e. is it not possible to use the newly generated slug?
I use this code mostly in generating some very specific and custom shortcode outputs, however, because of the issues I am running into I have dialled it right back to the version I have given here and am running at init with priority 15 just to purely test the code. I've also tried moving this to the functions.php in the theme and deactivated all plugins except for Types.
I've also looked in the DB at the new tables, specifically wp_toolset_relationships and wp_toolset_associations and all the data seems to be set to return 1 post for this query. See attached.
OK thanks for that. Was kind of hoping that I could just use the relationship slug for legacy and new relationships, since the database stores what post types are related and in what relationship, I actually don't really see why I can't. In this set up I'm using I actually put this WP_Query in a fairly generic function for getting related posts, so this seems like my code is going to have to identify the type of relationship - i.e. legacy or not - before generating the WP_Query $args? Is this really right?
Any further comments on assistance on why my query is not working here as per my last post? As I can't see what I have not done correctly.
I'm not sure why this isn't working as I see no reason for it not to be working. I also checked that the relationships were also migration from our Legacy methods.
OK, I believe I have found the issue here. These queries do not work on a site that uses a custom database table prefix.
In this file - vendor/toolset/toolset-common/inc/autoloaded/wp_query_adjustments/table_join_manager.php - you guys have hard coded the wp_ prefix in multiple locations...
Actually it occurs a number of times, it appears that there are a number of references to the (hard coded) wp_posts table, with inner joins to a table stored in the variable $associations_table_alias which does appear to be looking up the table prefixes correctly.
The lines that need to be changed are:
Line 100 - "wp_posts.ID"
Line 126 - "wp_posts.ID"
Line 127 - "wp_posts.post_type"
Line 136 - "wp_posts.ID"
NB. these are the ones that I found by searching the plugin directory. Perfectly possible that there are more but I think your devs need to check this properly and make sure that you aren't hard coded table prefixes!!
Thanks Shane. No that function doesn't work either, however, some others I've tried, e.g. toolset_get_related_post_types() does work, but assuming this is because it doesn't required the wp_posts table, and it does seem that any references to the new relationship tables do seem to pick up the table prefixes correctly.
@Shane: Have you any idea when this issue will be resolved? As I am stuck with the same problem - my tables' prefixes are also different from wp_ and I am receiving the same error as dominicV when trying to use the new toolset_relationships query argument.