Opened 7 years ago
Closed 4 years ago
#38360 closed defect (bug) (worksforme)
WP does not return attached media
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.6.1 |
Component: | General | Keywords: | |
Focuses: | Cc: |
Description
Hi.
I spent few thinking why my function does not work. Then I started debugging SQL queries.
I've got few pictures attached to my post.
I used get_children(get_the_ID()
and I got nothing.
Similar effect with get_attached_media and get_posts with arguments (like post_parent).
What I noticed?
SQL query from get_children looks like the below:
SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_parent = 1510 AND wp_posts.post_type IN ('post', 'page', 'attachment', 'teams', 'projects', 'technologies-list') AND ((wp_posts.post_status = 'publish' OR wp_posts.post_status = 'pending')) ORDER BY wp_posts.post_date DESC
I started thinking it could be wrong, because post_status of the images is inherit. I changed the status in DB to publish and it started working.
I thought I figured out where the reason is, but now I'm no longer sure.
I'm sure in the above query wp_posts.post_status = 'inherit'
is omitted for post_type=attachment.
In get_posts()
you set post_status
to 'inherit'
if it is attachment but it seems not to work, even if I pass post_status to the function.
Then the query is built and post_status is checked if it is equal to 'publish'
and it should be checked if it is inherit and if so the status of its parent should be checked.
If you have any question I'm at your service.
Kind regards,
Bernard
Change History (7)
#2
in reply to:
↑ 1
@
7 years ago
Hi @joemcgill,
take a look below.
This is my SQL query
SELECT wp_posts.* FROM wp_posts WHERE 1 = 1 AND wp_posts.post_parent = 1510 AND wp_posts.post_type IN ('post', 'page', 'attachment', 'teams', 'projects', 'technologies-list') AND ((wp_posts.post_status = 'publish' OR wp_posts.post_status = 'pending')) ORDER BY wp_posts.post_date DESC;
$r
array in get_children()
is
Array ( [numberposts] => -1 [post_type] => any [post_status] => any [post_parent] => 1510 )
$r array in
get_posts() after
$r = wp_parse_args( $args, $defaults );`
Array ( [numberposts] => -1 [category] => 0 [orderby] => date [order] => DESC [include] => Array ( ) [exclude] => Array ( ) [meta_key] => [meta_value] => [post_type] => any [suppress_filters] => 1 [post_status] => any [post_parent] => 1510 )
And this is from the clean version
SELECT wp_posts.* FROM wp_posts WHERE 1 = 1 AND wp_posts.post_parent = 1 AND wp_posts.post_type IN ('post', 'page', 'attachment') AND ((wp_posts.post_status <> 'trash' AND wp_posts.post_status <> 'auto-draft')) ORDER BY wp_posts.post_date DESC;
where the same $r
is
Array ( [numberposts] => -1 [post_type] => any [post_status] => any [post_parent] => 1 )
$r array in
get_posts() in clean WP after
$r = wp_parse_args( $args, $defaults );`
Array ( [numberposts] => -1 [category] => 0 [orderby] => date [order] => DESC [include] => Array ( ) [exclude] => Array ( ) [meta_key] => [meta_value] => [post_type] => any [suppress_filters] => 1 [post_status] => any [post_parent] => 1 )
Both WP are 4.6.1
The problem is here:
AND ((wp_posts.post_status = 'publish' OR wp_posts.post_status = 'pending'))
If I replace the above line to:
((wp_posts.post_status <> 'trash' AND wp_posts.post_status <> 'auto-draft'))
my query returns attachments as it is expected.
Hope this helps.
#3
@
7 years ago
Thanks for the quick reply @blatan. Unfortunately, I still don't understand what you're doing to encounter the issue. Are you building your own SQL queries, or are you trying to demonstrate that the SQL generated by get_children()
or get_posts()
whenever you pass the specific arguments you are suggesting?
#5
in reply to:
↑ 4
@
7 years ago
Replying to blatan:
I just show what the functions generated.
Could you share the exact code you're using to produce what is generated?
#6
@
7 years ago
- Keywords close added
The query from your clean version looks correct. The one you originally posted doesn't, so it seems like a plugin or theme is altering your post_status
at some point (pre_get_posts
, etc.). You could try and disable your plugins one by one until the function starts working as expected, that will help you narrow it down.
#7
@
4 years ago
- Keywords close removed
- Milestone Awaiting Review deleted
- Resolution set to worksforme
- Status changed from reviewing to closed
Since this has not been updated in over 2 years, I am going to close this out. But, @blatan, if you are still experiencing this issue, please reopen and provide more specific details and code examples so someone else can reproduce the issue.
Hi @blatan,
Thanks for the report. I'm unable to reproduce this in with a post that has a few images attached. In my test case both
get_children( $post_id )
andget_attached_media( 'image', $post_id )
is returning an array containing the attachment posts. Could you check if this is happening with a clean install of WordPress running a default theme and all plugins disabled?Thanks,
Joe