Home > Net >  WP_Query with same args producing different SQL statement in frontend and backend
WP_Query with same args producing different SQL statement in frontend and backend

Time:01-13

I have built a shortcode in Wordpress that displays media files from a particular custom taxonomy. The code works correctly on the backend when using "Shortcake (Shortcode UI)" to render the code in the classic Editor. On the frontend, however, it produces no results.

I have discovered that the SQL statement that is generated by WP_Query is different on the backend and the frontend, even though the args are identical. I don't understand why this is happening.

Here are the $args:

$args = array(
    'post_type' => 'attachment',
    'post_status' => 'any',
    'tax_query' => array(
        array(
            'taxonomy' => 'media_folder',
            'field'    => 'term_id',
            'terms'    => 385
        ),
    ),
);
$the_query = WP_Query($args);

When I print_r($the_query) and look at the results inside the Shortcode UI wrapper, this is the final query I see and this works as expected:

/*BACK END*/
SELECT    SQL_CALC_FOUND_ROWS wp_posts.id
FROM      wp_posts
LEFT JOIN wp_term_relationships
ON        (
                wp_posts.id = wp_term_relationships.object_id)
LEFT JOIN wp_posts AS p2
ON        (
                wp_posts.post_parent = p2.id)
WHERE     1=1
AND       (
                wp_term_relationships.term_taxonomy_id IN (385) )
AND       wp_posts.post_type = 'attachment'
AND       ( ( (
                                    wp_posts.post_status <> 'trash'
                          AND       wp_posts.post_status <> 'auto-draft' )
                OR        (
                                    wp_posts.post_status = 'inherit'
                          AND       (
                                              p2.post_status <> 'trash'
                                    AND       p2.post_status <> 'auto-draft'))))
GROUP BY  wp_posts.id
ORDER BY  wp_posts.post_date DESC
LIMIT     0, 5

On the frontend of the site, here is the final query generated, and it produces no results:

/*FRONT END*/
SELECT    SQL_CALC_FOUND_ROWS wp_posts.id
FROM      wp_posts
LEFT JOIN wp_posts AS p2
ON        (
                wp_posts.post_parent = p2.id)
WHERE     1=1
AND       (
                0 = 1 )
AND       wp_posts.post_type = 'attachment'
AND       ( ( (
                                    wp_posts.post_status <> 'trash'
                          AND       wp_posts.post_status <> 'auto-draft' )
                OR        (
                                    wp_posts.post_status = 'inherit'
                          AND       (
                                              p2.post_status <> 'trash'
                                    AND       p2.post_status <> 'auto-draft' ) ) ) )
GROUP BY  wp_posts.id
ORDER BY  wp_posts.post_date DESC
LIMIT     0, 5

The frontend version of the query omits any reference to the 'wp_term_relationships' table, and I cannot for the life of me figure out why. Or why the SQL Statement should be any different given the exact same arguments.

What am I missing?

CodePudding user response:

  1. Check if Taxonomy is plubic
  2. Check if any of the plugin or theme is using pre_get_post filter which might modify the final query result.

2nd option might not be pratical if you have a site with large number of plugins but try to shortlist plugin by its functionality like any search plugin.

  •  Tags:  
  • Related