Get posts with a specific relationship ID
Support › MB Relationships › Get posts with a specific relationship ID
- This topic has 3 replies, 2 voices, and was last updated 12 months ago by
Peter.
-
AuthorPosts
-
October 30, 2024 at 11:18 PM #46792
pza
ParticipantIt seems like there's no way to query for objects that simply have a relationship ID.
It would be great if the 'relationship' query argument could accept EXISTS or NOT EXISTS. Similar to meta_query. Instead of passing it via "to" or "from" maybe introduce the "compare" param?
So for a modified example based on the documentation, something like this:
$query = new WP_Query( [ 'relationship' => [ [ 'id' => 'events_to_bands', 'compare' => "EXISTS" ], ], ] );Or if there is some current way to get such posts, please let me know. Thanks!
October 31, 2024 at 11:37 PM #46796Peter
ModeratorHello,
Thank you for your feedback.
There isn't an argument like
'compare' => "EXISTS"in the relationship query. You need to define where to get the relationshipfromorto. For example: you have 4 posts: A, B, C, D with the connection A-B, C-D and belong to the relation "alphabet".
So if you just query to get the objects/posts from the relation "alphabet", you cannot know which one is connected to another one. Then the relationship doesn't mean anything.In your case, I think you should use a category for those posts and query to get posts by category.
November 1, 2024 at 12:06 AM #46797pza
ParticipantHi and thanks for your reply. I understand how Relationships work, and have used them successfully in various cases.
This time I was going for this:
- Profiles are already assigned to Category taxonomy
- I'm using a relationship "profile_featured_in_category" to indicate if the profile should be highlighted, not only classified in the Category.Also I would be able to query "all featured profiles" regardless of which Category or Profile.
I know I could do this with a tax_advanced field, and may have to. But then I lose out on the benefits of Relationships.
Thanks again.
November 1, 2024 at 11:00 PM #46808Peter
ModeratorHello,
If you have a Profile post and Category taxonomy, then you want to
featureda post, simply, just create another taxonomy and assign the term to the Profile post. I think it is easier for you to set it up and no need to use MB features: relationship, custom field ... to query all Profile posts based on a taxonomy.
Otherwise, if you use MB relationship, you can create a custom SQL query to get all object IDs that belong to a relationship ID. Refer to the previous topic https://support.metabox.io/topic/get-posts-with-no-relationship/#post-44286 -
AuthorPosts
- You must be logged in to reply to this topic.