Custom Fields
Re-Envisioned
Support MB Relationships WP_Query Multiple Relationships relation = AND doesn't work ?

  • Creator
    Topic
  • #24662
    Resolved Julien
    Participant

    Hi,

    since yesterday I tried to make a WP_Query with 2 relationship with a AND relation.

    Here is the content of the array of my query.

    Array
    (
        [post_type] => recipe
        [paged] => 1
        [posts_per_page] => 9
        [relationship] => Array
            (
                [0] => Array
                    (
                        [id] => recipes_to_brands
                        [to] => 75
                    )
    
                [1] => Array
                    (
                        [id] => cheeses_to_recipes
                        [from] => 73
                    )
    
                [relation] => AND
            )
    
    )
    

    With that, no result.

    If I exclude one of the two relation, I have my result.

    So with

    Array
    (
        [post_type] => recipe
        [paged] => 1
        [posts_per_page] => 9
        [relationship] => Array
            (
                [0] => Array
                    (
                        [id] => recipes_to_brands
                        [to] => 75
                    )
    
                [relation] => AND
            )
    
    )

    And

    Array
    (
        [post_type] => recipe
        [paged] => 1
        [posts_per_page] => 9
        [relationship] => Array
            (
                [0] => Array
                    (
                        [id] => cheeses_to_recipes
                        [from] => 73
                    )
    
                [relation] => AND
            )
    
    )

    I have each time the same post found.

    If I replace AND by OR, my result is found but I don't want to use an OR 😉

    Wordpress Core, Metabox and Meta Box Aio are on last version.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Author
    Replies
  • #24668
    Long Nguyen
    Moderator

    Hi,

    I will check this case and let you know later.

    Thank you.

    #24671
    Julien
    Participant

    Hello,

    thanks,

    I did a video record with a quick sample test code.

    <?php
    // Include this page in your functions.php
    // Flush permalinks
    // Create 1 Recipe
    // Create 1 Brand
    // Create 1 Cheese
    // On your recipe, associate created brand and created cheese
    // Adapt id's on adaptQuery function with correct ids
    // Go to "http://www.domain.com/recipe/
    // You will see "Nothing here"
    // If you comment $brand or $cheese, you will "see" your created recipe
    // If you change AND by OR, you will see your created recipe twice
    add_action('init', 'createCustomPostType', 10);
    add_action('mb_relationships_init', 'myRelationships', 10);
    add_action('parse_query', 'adaptQuery', 5, 1);
    function createCustomPostType(){
    $labels = array(
    'name' => 'Recipes',
    'singular_name' => 'Recipe',
    'menu_name' => 'Recipes',
    );
    $args = array(
    'labels' => $labels,
    'description' => 'Recipes',
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'query_var' => true,
    'capability_type' => 'post',
    'has_archive' => true,
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array(
    'title',
    'editor',
    )
    );
    register_post_type('recipe', $args);
    $labels = array(
    'name' => 'Brands',
    'singular_name' => 'Brand',
    'menu_name' => 'Brands',
    );
    $args = array(
    'labels' => $labels,
    'description' => 'Brands',
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'query_var' => true,
    'capability_type' => 'post',
    'has_archive' => false,
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array(
    'title',
    'editor',
    )
    );
    register_post_type('brand', $args);
    $labels = array(
    'name' => 'Cheeses',
    'singular_name' => 'Cheese',
    'menu_name' => 'Cheeses',
    );
    $args = array(
    'labels' => $labels,
    'description' => 'Cheeses',
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'query_var' => true,
    'capability_type' => 'post',
    'has_archive' => false,
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array(
    'title',
    'editor',
    )
    );
    register_post_type('cheese', $args);
    }
    function myRelationships(){
    MB_Relationships_API::register( [
    'id' => 'recipes_to_brands',
    'from' => 'recipe',
    'to' => 'brand',
    ]);
    MB_Relationships_API::register( [
    'id' => 'cheeses_to_recipes',
    'from' => 'cheese',
    'to' => 'recipe',
    ]);
    }
    function adaptQuery($query){
    if ( !is_admin() && $query->is_main_query() ) {
    if ( is_post_type_archive('recipe')) {
    $cheese = 17; // My cheese ID
    $brand = 18; // My brand ID
    $relationShipArgs = [];
    if(isset($brand)):
    $relationShipArgs[] = [
    'id' => 'recipes_to_brands',
    'to' => $brand,
    ];
    endif;
    if(isset($cheese)):
    $relationShipArgs[] = [
    'id' => 'cheeses_to_recipes',
    'from' => $cheese,
    ];
    endif;
    if(sizeof($relationShipArgs) > 0):
    $relationShipArgs['relation'] = 'AND';
    $query->set('relationship', $relationShipArgs);
    endif;
    }
    }
    return $query;
    }

    Thank you

    #24815
    Julien
    Participant

    Hello,

    have you been able to look at this ?

    Sorry to insist but it's a little bit problematic for me.

    #27156
    Julien
    Participant

    Hi,

    1 month since my last answer.

    Any news ?

    #27166
    Long Nguyen
    Moderator

    Hi,

    I've added this case to the backlog development, I will get back to you if I have any information.

    #28909
    Leerpodium
    Participant

    Hi, we have this exact same problem.
    Do you have an update regarding this issue?

    #28912
    Cereal Concept
    Participant

    Hi !

    I have the same problem.
    Works well with OR but not with AND

    Can you look into it when you have more time ?

    Thanks !

    #29104
    Julien
    Participant

    I discover this issue since 18 february and still any fix...

    Why is so long ?

    #29107
    Long Nguyen
    Moderator

    Hi friends,

    We are working tirelessly on this issue. Please check the commits in the repository https://github.com/wpmetabox/mb-relationships/commits/master

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