Get posts with a specific relationship ID

Support MB Relationships Get posts with a specific relationship ID

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #46792
    pzapza
    Participant

    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"
            ],
        ],
    ] );

    Or if there is some current way to get such posts, please let me know. Thanks!

    #46796
    PeterPeter
    Moderator

    Hello,

    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 relationship from or to. 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.

    #46797
    pzapza
    Participant

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

    #46808
    PeterPeter
    Moderator

    Hello,

    If you have a Profile post and Category taxonomy, then you want to featured a 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

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