Skip Navigation

[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, 5 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 14 replies, has 4 voices.

Last updated by monaB 6 years, 5 months ago.

Assisted by: Shane.

Author
Posts
#911249

I am trying to update my various queries to make use of the new relationships as well as the old, now migrated, relationships using the information here - https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#wp_query-argument-for-querying-by-related-posts

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 have tried:

  $args = array(
    'numberposts' => -1,
    'post_type' => 'climma-kit',
    'toolset_relationships' => array(
      array(
        'role' => 'child',
        'related_to' => 795,
        'relationship' => 'cabin_climma-kit',
      ),
    ),
  );

  $query = new WP_Query( $args );
  $args = array(
    'numberposts' => -1,
    'post_type' => 'climma-kit',
    'toolset_relationships' => array(
      array(
        'role' => 'child',
        'related_to' => 795,
        'relationship' => array( 'cabin' , 'climma-kit' ),
      ),
    ),
  );

  $query = new WP_Query( $args );

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.

What am I missing?

#911389

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:

$query = new WP_Query( 
    array(
        'post_type' => 'toolset-child',
        'numberposts' => -1,
        'toolset_relationships' => array(
            array(
                'role' => 'child',
                'related_to' => 8,
                'relationship' => array( 'toolset-parent', 'toolset-child' )
            )
        ),
    )
);
$posts = $query->posts;
var_dump($posts);

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.

#911939
Picture2.png
Picture1.png

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.

#912046

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dominic,

Yes you would need to use the array format because you need to specify the 2 post type that are in the relationship.

Thanks,
Shane

#912221

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.

#913054

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dominic,

When you say relationship slug you are referring to "_wpcf_belongs_writer_id" as an example correct.

The new method doesn't work for you ?

You code seems fine to me.

Would you mind allowing me to have access to the site where you have set this up ?

Thanks,
Shane

#913158

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dominic,

Your query actually works fine but I see you're trying to get the post count , as in the number of child posts correct?

Please let me know.
Thanks,
Shane

#913160

That's correct, I was dumping the post count as a test, but this was returning 0, and the result should be 1!

#913625

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dominic,

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.

Could you try the function methods instead of directly using the a query.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Thanks,
Shane

#913898

Hi,

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...

I think this needs to be fixed your end...

Thanks,

#914086

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dominic,

Could you let me know the exact line number that you found this fault?

Thanks,
Shane

#914586

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!!

#914917

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dominic,

I've reported this.

However does the function here works? https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Please let me know.
Thanks,
Shane

#915133

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.

#948296
Screen Shot 2018-07-13 at 13.41.25.png

@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.