Get posts with no relationship

Support MB Relationships Get posts with no relationship

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #44283
    Topher DeRosiaTopher DeRosia
    Participant

    I 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?

    #44286
    PeterPeter
    Moderator

    Hello,

    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 from or to based 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-parameters

    #46785
    pzapza
    Participant

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

    #46791
    PeterPeter
    Moderator

    Hello 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?

    #46793
    pzapza
    Participant

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

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.