Custom Fields
Re-Envisioned
Support MB Relationships Query to only show posts which are connected

This topic contains 3 replies, has 2 voices, and was last updated by  Max@powdersky.com 4 months, 4 weeks ago.

  • Creator
    Topic
  • #13035
    Resolved
    Max@powdersky.com
    Participant

    Hi,

    I want to only show a specific custom post type (resort) if it has a connected CPT (Accommoation)

                     $resort_args = array(
                       'post_type' => 'resort',
                       'posts_per_page' => -1,
                       'tax_query' => array(
                         array(
                           'taxonomy' => '_resort_country',
                           'field'    => 'id',
                           'terms'    => $country->term_id
                         ),
                       ),
                       'relationship' => array(
                            'id' => 'accommodation_to_resorts',
                            'compare' => 'EXSISTS',
                            //This is an example not working
                        ),
                     );
      MB_Relationships_API::register( array(
            'id'   => 'accommodation_to_resorts',
            'from'   => array(
                        'object_type' => 'post',
                        'post_type'   => 'resort',
                        'meta_box'    => array(
                            'hidden'         => 'true',
                        ),
                    ),
                    'to'   => array(
                        'object_type' => 'post',
                        'post_type'   => 'accommodation',
                        'meta_box'    => array(
                            'title'         => 'Resort',
                            'context'       => 'side',
                            'empty_message' => 'No resorts.',
                        ),
                    ),

    Thanks in advanced

Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #13040

    Anh Tran
    Keymaster

    Hi Max,

    Querying posts using relationship as a condition is not supported. You might want to query posts as usual, then perform an API call to get connected items for each post. Then you can use condition to decide whether to show them.

    MB_Relationships_API::each_connected( array(
        'id'   => 'posts_to_pages',
        'from' => $wp_query->posts, // 'from' or 'to'.
    ) );
    
    while ( have_posts() ) : the_post();
        // Skip if no connected posts.
        if ( empty( $post->connected ) ) {
            continue;
        }
    
        // Output here
    endwhile;
    #13048

    Max@powdersky.com
    Participant

    Perfect thanks Anh, will give it a go and let you know the outcome.

    #15092

    Max@powdersky.com
    Participant

    OK sorry just managed to get on to this, that all worked just need to make sure to include global $post; ! 🙂

Viewing 3 replies - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.