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'