Opened 10 years ago
Closed 10 years ago
#27674 closed defect (bug) (wontfix)
WP_Query auto-draft
Reported by: | Philo01 | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 3.8.1 |
Component: | Query | Keywords: | |
Focuses: | Cc: |
Description
It is not possible to query a single post that has the 'auto-draft' status.
$property = new WP_Query( 'p=1&post_type=property&post_status=auto-draft' ); // 0 results $property = new WP_Query( 'post_type=property&post_status=auto-draft' ); // 10 results
The code responsible for this can be found in query.php L:2972 - 3002
Change History (15)
#3
@
10 years ago
- Keywords reporter-feedback added
In your first example, is there a post with ID = 1 and post_status = 'auto-draft'?
In your second example, is there a post with ID = 1 and post_type = 'attachment'?
When using p=1
only this post is searched for. If you want to find auto-drafts or attachments belonging to post with ID 1 you will have to set post_parent=1
.
#5
@
10 years ago
I cannot reproduce this problem.
I created a post record with ID = 513, post_type = property, post_status = auto-draft
$q = new WP_Query( 'p=513&post_type=property&post_status=auto-draft' ); var_dump( $q->found_posts );
Result: int(1)
I don't know when, or if ever, a post gets post_status auto-draft in core. I know there are posts with post_type = 'revision'
for revisions.
#6
@
10 years ago
I'm allowing users to create a new property on the front-end side, so in order to attach images etc, I'm using the same approach as the back-end, by creating an auto-draft. If they submit the form, but it didn't pass the validation they are returned and the original values are loaded via the property id.
<?php if($property_id = $input->get('property-id')) // $_GET { $property = new WP_Query( 'p=' . $property_id . '&post_type=property&post_status=auto-draft' ); } else { $property = get_default_post_to_edit( 'property', true ); } <form action="<?php echo add_query_arg(array('property-id' => $property->ID), get_permalink()); ?>" method="post">
When I try to access an existing property via ?property-id=123 it won't work. It results in 0 rows, if I run the generated MySQL query against the database it does return 1 row. Could you try and see if you can reproduce the problem with the code above? Thanks
#7
@
10 years ago
- Keywords close added; reporter-feedback removed
It seems you expect WP_Query to return a post object. I would have used get_posts()
to return an array of posts. If you need more help, please try try the support forumshttp://wordpress.org/support/.
#8
@
10 years ago
- Keywords 2nd-opinion added; close removed
Thanks for your reply.
I know the results are different, after the WP_Query I would fetch the post from the results.
Anyway I still feel this is a bug, so I made a small screencast to show it: http://d.pr/v/aRY6
Code:
<?php $post_id = (isset($_GET['post-id'])) ? $_GET['post-id'] : false; if($post_id) { $post = new WP_Query( 'p=' . $post_id . '&post_type=post&post_status=auto-draft' ); echo '<h3>WP_Query:</h3><hr>'; var_dump($post); } else { require_once( ABSPATH . 'wp-admin/includes/post.php' ); $post = get_default_post_to_edit( 'post', true ); echo '<h3>get_default_post_to_edit:</h3>'; echo '<a href="?post-id=' . $post->ID . '">Post ' . $post->ID . '</a><hr>'; var_dump($post); } ?>
As you can see the WP_Query returns 0 posts, while the raw query returns 1 post.
#9
@
10 years ago
Auto drafts are an internal construct. I don't think WP_Query or any API except get_default_post_to_edit() should ever expose these.
If they are submitting a form, then it should become a 'draft'. Auto drafts are only so we have an ID to work with immediately, before anything happens and they are creating something new.
#10
@
10 years ago
Thanks for your reply!
That's to bad :( , the cool thing about get_default_post_to_edit is that these drafts are automatically removed and like you said, that you have an ID to work with right away. I use the ID to attach images which are uploaded via AJAX.
If I would set the status to draft, it would possibly result in a bunch of empty entries which will be visible in the back-end.
What about attachments though? It shows the same results as in the screencast.
$attachment = new WP_Query( 'p=1&post_type=attachment&post_status=inherit' ); // 0 results $attachment = new WP_Query( 'post_type=attachment&post_status=inherit' ); // 10 results
Just wondering why it's possible to fetch 10 auto-drafts or 10 attachments and not 1?
#11
@
10 years ago
I run
SELECT * FROM `wp_posts` WHERE `post_type` = 'attachment' AND `post_status` ='inherit'
and get 32 results. The first ID = 72
I run
$query = new WP_Query( 'p=72&post_type=attachment&post_status=inherit' ); echo $query->found_posts;
and it gives me 1 result as expected.
Have you really tried this with no active plugins and a clean default theme?
#12
@
10 years ago
I know $query->found_posts; returns 1, but if you take a look at the posts array you will see that this is empty.
public 'posts' => array (size=0) empty public 'post_count' => int 0
The complete var_dump() http://laravel.io/bin/jd1G
All tested on a clean install with no modifications / plugins whatsoever.
#14
@
10 years ago
I'm currently using get_post() but WP_Query seemed easier, because with get_post() you can't specify any rules, so now you need to do a few if statements to make sure it's an attachment, from the correct author etc.
#15
@
10 years ago
- Keywords 2nd-opinion removed
- Milestone Awaiting Review deleted
- Resolution set to wontfix
- Status changed from new to closed
Philo01, thanks for the report, but I don't see this picking up any steam. Writing application code around Auto Drafts probably does more future harm than good. Your ideas are always welcome, so feel free to open tickets whenever you feel like another change needs to be made.
The same goes for trying to query an attachment.
So it seems when is_single = true, the posts array is being reset.