Get posts with no relationship
Support › MB Relationships › Get posts with no relationship
- This topic has 4 replies, 3 voices, and was last updated 12 months ago by
pza.
-
AuthorPosts
-
January 13, 2024 at 4:42 AM #44283
Topher DeRosia
ParticipantI have a relationship set up between Posts and a CPT. I want to query for posts that don't have a connection. How can I do that?
January 13, 2024 at 10:45 PM #44286Peter
ModeratorHello,
This is an interesting question. Currently, we don't have a setting to get posts that don't have a connection. I suggest you try to create a custom code to do that:
1. Create an SQL query: get the post ID from the column
fromortobased on the relationship ID. You will have an array of posts that have a connection.2. Create a WP Query and pass the array to the argument
post__not_in. Read more about this argument in the documentation
https://developer.wordpress.org/reference/classes/wp_query/#post-page-parametersOctober 30, 2024 at 2:26 AM #46785pza
ParticipantHey, I'm making a suggestion here instead of a new post.
It 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" ], ], ] );If this is not feasible, is there already a way to query all objects with a particular relationship?
Now I'm using Relationships to "feature" a post in a category (which they are, separately, also categorized in).
But if I can't query all related (featured) posts, I will have to use a field like "taxonomy_advanced" field which is less ideal for my case.
Thanks!
October 30, 2024 at 10:47 PM #46791Peter
ModeratorHello pza,
I think your case doesn't relate to the case in this topic which is getting posts with no relationship. It would be better if you create a new topic and post your question there.
If you have posts and category, you can use the taxonomy field to select the category. Create a specific term "feature" and select the term when editing the post. Would it be easier without using a relationship?
October 30, 2024 at 11:21 PM #46793pza
ParticipantI thought "exists" and "not exists" were not very different. But sure, I'll make another topic.
Using advanced taxonomy is possible but misses out on the advantages of Relationships, like performance and being able to view the connection from both sides.
-
AuthorPosts
- You must be logged in to reply to this topic.