MB Relationship WP_Query no longer works in Meta Box AIO 1.14.1 and 1.14.2

Support MB Relationships MB Relationship WP_Query no longer works in Meta Box AIO 1.14.1 and 1.14.2Resolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #29767
    Paul WenzelPaul Wenzel
    Participant

    This worked prior to Meta Box AIO 1.14.1 and 1.14.2:

    $connected = new WP_Query( [
      'relationship' => [
        'id'   => 'podcasts_to_episodes',
        'from' => get_the_ID(), // the ID of the podcast (a custom post type)
      ],
      'orderby' => 'date',
      'order' => 'DESC',
      'paged' => $paged,
      'post_status' => 'publish'
    ] );

    Now $connected->post_count results in 0.

    I also tried MB_Relationships_API::get_connected but it also yielded zero results.

    Do I need to change anything about my code to support the latest version of MB Relationships?

    Thanks!

    #29775
    Long NguyenLong Nguyen
    Moderator

    Hi Paul,

    I've tested the query by relationship again but not see any issue. Can you please share the code that creates the relationship? And re-check the episode posts are published.

    #29809
    Paul WenzelPaul Wenzel
    Participant

    Here's some snippets from my Podcast to Episodes relationship. As far as I can tell, this part continues to work fine in the last few updates of Meta Box AIO. Something is different about the way related posts are queried, however.

    
    // register relationship
    add_action('mb_relationships_init', array($this,'podcasts_to_episodes_relationship'));
    
    function podcasts_to_episodes_relationship () {
        MB_Relationships_API::register( array(
            'id'   => 'podcasts_to_episodes',
            'reciprocal' => true,
            'from' => array(
                'object_type'  => 'post',
                'post_type'    => 'episode',
                'admin_column' => true,
                'meta_box'     => [
                    'title' => 'Podcast',
                ],
            ),
            'to' => array(
                'object_type'  => 'post',
                'post_type'    => 'podcast',
                'admin_column' => false,
            ),
        ));
    }
    
    ...
    
    // link episode id to podcast id
    MB_Relationships_API::add( $episode_id, $podcast_id, 'podcasts_to_episodes');
    #29818
    Long NguyenLong Nguyen
    Moderator

    Hi Paul,

    Can you please check a post Episode if that is connected to a post Podcast when editing the post? It is possible that the connection is not set then the query does not find the post connected.

    If you call the method add in your class, it should be hooked to the action mb_relationships_init

    add_action( 'mb_relationships_init', function () {
        MB_Relationships_API::add( 123, 456, 'podcasts_to_episodes');
    } );
    #29855
    Paul WenzelPaul Wenzel
    Participant

    I was able to solve the issue by passing 'post_type' => 'episode' with my WP_Query call noted above.

    It now looks like this:

    $connected = new WP_Query( [
      'relationship' => [
        'id'   => 'podcasts_to_episodes',
        'from' => get_the_ID(), // You can pass object ID or full object
      ],
      'orderby' => 'date',
      'order' => 'DESC',
      'paged' => $paged,
      'post_status' => 'publish',
      'post_type' => 'episode'
    ] );

    I wasn't using this before, but it looks like that resolves my issue. The post_type parameter appears to be required after this change in inc/query/post.php, introduced in MB Relationships 1.10.6:

    https://github.com/wpmetabox/mb-relationships/commit/e48e19322b4d9e13e3942c78fd27278a311e8080

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