0

I have a function to retrieve a random set of pictures according to the category. This has been working when I set up the website last year. Now I tested the site today and found out that none of my random pictures show up.

In my function I used WP_Query to query the WP DB. WP_Query no longer returns any results.

A print_r of the array passed to the WP_Query looks like this:

 Array
(
    [fields] => ids
    [post_status] => inherit
    [post_type] => attachment
    [post_mime_type] => Array
        (
            [0] => image/jpeg
            [1] => image/png
        )

    [category_name] => landstrasse+verkehr
    [tag] =>
    [post__not_in] => Array
        (
            [0] =>
        )

    [nopaging] => 1
    [posts_per_page] => -1
    [orderby] => rand
)

The resulting SQL Query looks like this:

SELECT   wpq5_12_posts.ID FROM wpq5_12_posts  
LEFT JOIN wpq5_12_term_relationships ON (wpq5_12_posts.ID = wpq5_12_term_relationships.object_id)  
LEFT JOIN wpq5_12_term_relationships AS tt1 ON (wpq5_12_posts.ID = tt1.object_id) 
WHERE 1=1  
AND wpq5_12_posts.ID NOT IN (0) 
AND (wpq5_12_term_relationships.term_taxonomy_id IN (37)   AND   tt1.term_taxonomy_id IN (9)) 
AND (wpq5_12_posts.post_mime_type = 'image/jpeg' OR wpq5_12_posts.post_mime_type = 'image/png')  
AND wpq5_12_posts.post_type IN ('post', 'page') 
AND ((wpq5_12_posts.post_status = 'inherit')) 
GROUP BY wpq5_12_posts.ID ORDER BY RAND()

The Select statement is wrong as the desired post type 'attachment' is not in the list: wpq5_12_posts.post_type IN ('post', 'page').

None of the solutions found in the WP_Query documentation worked for me. Whatever combination I entered in the post_type or post_status, that according to the WP_Query documentation should work did not yield any results.

The SQL Query gives me the desired results if I replace wpq5_12_posts.post_type IN ('post', 'page') with: wpq5_12_posts.post_type = 'attachment'

1
  • The WP_Query looks correct. Have you disabled all your plugins and reverted to a default theme? While I wouldn't recommend doing this on a live site, you could utilize the Health Check & Troubleshooting Plugin to do only for your admin user. – Howdy_McGee Mar 12 at 16:02
0

From further tests it is now certain that WP_Query has been altered at some point in the last year to render it useless for searching attachments. As soon as you enter search criteria like categories or tags it always replaces the search for post type to: '.. IN ( post, page )' or If you enter a tax_query for category and attachments you get the following SQL:

WP_Query array:

Array
(
    [fields] => ids
    [post_status] => inherit
    [post_type] => attachment
    [post_mime_type] => Array
        (
            [0] => image/jpeg
            [1] => image/png
        )

    [category_name] => bauerndorf
    [tax_query] => Array
        (
            [relation] => OR
            [0] => Array
                (
                    [taxonomy] => category
                    [field] => slug
                    [terms] => Array
                        (
                            [0] => Array
                                (
                                    [0] => bauerndorf
                                )

                        )

                    [operator] => IN
                )

        )

    [post__not_in] => Array
        (
            [0] =>
        )

    [nopaging] => 1
    [posts_per_page] => -1
    [orderby] => rand
)

Resulting SQL. No sign of searching for taxonomy however the third line following the WHERE statement is a killer criteria: 'AND ( 0 = 1 )' can never become true!!

SELECT wpq5_12_posts.ID FROM wpq5_12_posts  
WHERE 1=1  
AND wpq5_12_posts.ID NOT IN (0) 
AND ( 0 = 1 )
AND (wpq5_12_posts.post_mime_type = 'image/jpeg' OR wpq5_12_posts.post_mime_type = 'image/png')  
AND wpq5_12_posts.post_type = 'attachment' 
AND ((wpq5_12_posts.post_status = 'inherit')) 
GROUP BY wpq5_12_posts.ID ORDER BY RAND() 

I gave up searching for a solution using WP_Query.

Now the only solution to get my page to work again is to adapt the existing SQL statement to my needs and use a wpdb->get.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.