Skip Navigation

[Resolved] Get related posts by author

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to get related posts, filtered by author, using the new post relationships API.

Solution: Use the standard WP_Query with toolset_relationships criteria and post author filter added to the query.

Relevant Documentation: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

This support ticket is created 6 years, 1 month 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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by takoL 6 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1106750

Tell us what you are trying to do?
I have a one to many relationship (chart -> data).
I need to get the related child posts (data posts) belonging to a parent (chart).
But I only need the posts of a certain author.

Is there any documentation that you are following?
Toolset relationships api

I tried using the toolset_get_related_posts() function but it does not allow to filter by author.
This would really be a great feature to include!

So i tried storing user info in a post meta field (wpcf-student-id) and adding it to the query arguments.

$records = toolset_get_related_posts(
            $chart, // get posts related to this one
            'chart-record', // relationship between the posts
            'parent', // get posts where post is the parent in given relationship
            999, //number of post
            0, // pagination
            array(
                'meta_key'      => 'wpcf-student-id',
                'meta_value'    => $_GET['uid'],
                'meta_compare'  => '=',
            ), // Additional query arguments
            'post_object' // return type
        );

This however does not return the child posts (It returns an empty array).
If I remove the meta query I do get all the child posts (not filtered by author), so I guess the problem is restricted to that part.
Am I using the meta query in the wrong way?

Any help would be very much appreciated.

Cheers,

Tako

#1106813

Hi, unfortunately post author isn't currently implemented in the toolset_get_related_posts API, but check out this document with examples showing how to implement toolset relationship criteria in a standard WP_Query:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/
You can add post author criteria to a WP_Query, which may achieve what you're looking for. Pay close attention to the remarks section at the bottom of the post for information about timing and when these filter options are available in the request lifecycle.

This however does not return the child posts (It returns an empty array).
One thing I can see is that there is no "role_name_to_return" argument supplied. It's required. After the 'post_object' parameter, add 'child'. But as I was saying, post author can be added to WP_Query along with toolset relationship criteria for a more robust, flexible query filter.

#1109009

Hi Christian,

Thank you for the reply.
Implementing the toolset relationships in the standard WP_Query is very powerful and fixes anything I needed to do.
Thanks for the help.

Cheers,

Tako