Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When tax_query is used in each_connected, only the first-ordered connected items will be put to respective connected counterpart #62

Open
simongcc opened this issue Apr 22, 2022 · 0 comments

Comments

@simongcc
Copy link

Here is the setup for the testing.

I have registered 2 custom post types, A and B and custom taxonomy for A say A_type.
Then I connect 3 A items to B.

There is 2 custom post types(skipped) and one custom taxonomy for A (A_type)
$slug = 'A_type';

MB_Relationships_API::register(
    array(
        'id'   => 'A_to_B',
        'from' => array(
            'object_type'  => 'post',
            'post_type'    => 'A',
            'admin_column' => 'after title',
        ),
        'to'   => array(
            'object_type'  => 'post',
            'post_type'    => 'B',
            'admin_column' => 'after title',
        ),
    )
);

$query_args = array(
    'post_type'      => 'B',
    'posts_per_page' => -1,
);

$all_Bs = new WP_Query( $query_args );

// This gives all results without problem, all connected A will be put into `connected_A`.
MB_Relationships_API::each_connected(
    [
        'id'       => 'A_to_B',
        'to'       => $all_Bs->posts,
        'property' => 'connected_A', // B post object result with this property
    ],
);

Up to this point, everything works fine.

If I want to filter specific taxonomy A_type from the above, I could add tax_query parameters.
I add the following into the each_connected 2nd argument.

[
    'tax_query' => array(
        array(
            'taxonomy'         => 'some_category_in_A',
            'field'            => 'term_id',
            'terms'            => 23,
            'include_children' => false,
        ),
    ),
],

Because according to the source code of each_connected, the 2nd argument takes usual WP_Query arguments which will be eventually put on WP_Query class to run.

However, the result have one problem.
Assume that there are 3 items from A connected to B.
Only the first one(In the dragging order of the relationship menu, only the top item) connected to respective A will be found and put in connected_A. i.e. If A1 is connected to B1, B2, B3 respectively, in the order of connection menu, if B2 is the first one. Then there is no connected_A result for B1 and B3 and the connection will be regarded and put to B2 connected_A only.
It seems that the query cannot see the other connected items with different order if introducing tax_query parameters.

I think it might be related to the table join(inner join, left join and so on), because I am not an expert to the MySQL language, my analysis could only be up to this point.
Hopefully someone could help in finishing the remaining bug fixing or limitation fixing work.

Thanks in advance.

For reference purpose, I have copied the query request from the var_dump for further investigation.

# This is the query without tax_query parameter
SELECT  prefix_posts.* , mbr.to AS `mbr_A_to_B_to` FROM prefix_posts  
INNER JOIN prefix_mb_relationships AS mbr ON  (mbr.from = prefix_posts.ID AND 
mbr.type = 'A_to_B' AND 
mbr.to IN (352,374,348,290,277,327,328))  
WHERE 1=1  AND ((
prefix_posts.post_type = 'A' AND 
(prefix_posts.post_status = 'publish' 
OR prefix_posts.post_status = 'private'))) 
GROUP BY `mbr_A_to_B_to`, prefix_posts.ID ORDER BY mbr.order_to 
# This is the query using tax_query parameter
SELECT  prefix_posts.* , mbr.to AS `mbr_A_to_B_to` FROM prefix_posts  
LEFT JOIN prefix_term_relationships ON (prefix_posts.ID = prefix_term_relationships.object_id) 
INNER JOIN prefix_mb_relationships AS mbr ON  (mbr.from = prefix_posts.ID AND 
mbr.type = 'A_to_B' AND 
mbr.to IN (352,374,348,290,277,327,328))  
WHERE 1=1  AND ( 
  prefix_term_relationships.term_taxonomy_id IN (23)
) AND ((prefix_posts.post_type = 'A' AND 
(prefix_posts.post_status = 'publish' 
OR prefix_posts.post_status = 'private'))) 
GROUP BY prefix_posts.ID, prefix_posts.ID ORDER BY mbr.order_to
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant