Skip Navigation

[Resolved] Get related posts filtered by the author parametr

This support ticket is created 3 years, 11 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.

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

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 6 replies, has 2 voices.

Last updated by Jamal 3 years, 11 months ago.

Assisted by: Jamal.

Author
Posts
#2179719

Hello.
I have Exhibits and Notes (CPT).
1. How can I select only author' Notes that linked to the Exhibit?

// it works
$my_video_id = toolset_get_related_posts(325, 'my-video-exhibit', 'child'); // works
var_dump($my_video_id);

// not works in any varaiations
$exhibit_id = 325;
$args = array(
'post_type' => 'my-videos',
'author' => 1,
'toolset_relationships' => array(
'role' => 'child',
'related_to' => 325,
'relationship' => 'my-video-exhibit'
)
);

$query = new WP_Query( $args );
$my_videos = $query->posts;
var_dump($my_videos);

#2179891

Update question

Hello.

I have Exhibits and Notes (CPT).
1. How can I select only author' Notes that linked to the Exhibit?

$my_video_id = toolset_get_related_posts(325, 'my-video-exhibit', 'child'); // works but without filtering by the author ID

2. How can I get posts without relationships?
It can be done in one WP_Query?

2.1 How can I get "list of IDs for all those posts which are in a relationship"?
https://toolset.com/forums/topic/filter-view-by-post-without-relationship/#post-1233800

#2180003

Hello and thank you for contacting Toolset support.

Indeed, you can't get these posts in one WP_Query. Toolset, in a view, uses two queries for this case. One query to get the related posts, then another query for author's posts, and post__in equals the related posts. Similar to:

$my_video_ids = toolset_get_related_posts(325, 'my-video-exhibit', 'child');
$args = array(
    'post_type' => 'my-videos',
    'author' => 1, // the author ID,
    'posts_per_page'   => -1, // to avoind the default posts per page.
    'post__in' = $my_video_ids,
);

$query = new WP_Query( $args );
$my_videos = $query->posts;

I hope this helps. Let me know if you have any questions.

#2180007

2. How can I get all posts without relationships? - is still actual
And/Or all post that have relationships.

#2180013

I think I have tested this a couple of months ago, but not today. Assuming that we have a one-to-many relationship (Oeuvres<>Coctails), with the slug "oeuvre-cocktail", this should do it:

    $oeuvres = get_posts( array(
      'post_type' => 'oeuvre',
      'numberposts'   => -1,
      'post_status'   => 'publish',
      'fields'    => 'ids'
  ) );

  $cocktails = toolset_get_related_posts( 
      array( 'parent' => $oeuvres ),
      'oeuvre-cocktail',
      array(
          'return' => 'post_object',
          'role_to_return' => 'child'
      )
  );

Does it make sense?

#2180749

Yes. If its work we can get all posts with relationship.
Can we have get all post without relationships?

Or we have the only 1 way to works with this two arrays to do it?

#2180871

Well, we currently do not have an API to get the posts that are not connected to a relationship. You need to find those connected posts and exclude them using the post__not_in argument.
https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters

If you believe this feature should be added to Toolset, please ask for it here https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

For support rules, we are able to handle only one issue at a time. This helps us to bring you a better service and also helps other users to find all the information here exposed. If you believe that your initial question has been answered, please mark this ticket as resolved and open a new ticket for any other questions/issues.