1

WP_Query is used to get posts via simple arguments provided by it. WordPress provides an argument called 's' for search. It basically searches content of all posts and lists the ones with that string anywhere. There is another WP_Query argument called, post_status, which can be used to filter posts, with particular Post_Status

Do note that this code is enclosed in AJAX function. I am calling an AJAX function from frontend, that passes the url of the current page to $_POST['url']. $_POST['url'] is defined and please don't worry about it. I want the Query to have only draft posts if the url has "draft/" in it. By echoing the variables I know, that the variables themselves are working fine.

So below is the WP_Query

$Current_Id =get_current_user_id();


echo $_POST['url'] . '/n';
if(strpos($_POST['url'], 'draft') == false ){
    $post_status = array('publish', 'privatised', 'draft', 'inherit', 'future');
}
else{
    $post_status = array('draft');
}

echo print_r($post_status) . '/n';

$args = array(
    'post_type'     => array('post', 'download', 'page'),
    'post_status'   => array('draft'),
    'author'        => $Current_Id,
    'numberposts'   => -1,
    'orderby'           => 'post_date',
    'order'             => 'DESC',
    'posts_per_page'    => 20,
    'paged'      => $_POST['pagenum'], 
    's'     => $_POST['search'],
 );

$pagenum = ($_POST['pagenum'] ? $_POST['pagenum'] : '1');
$user_posts = new WP_Query( $args);

    if( !empty($user_posts)){   
    ?>

    <?php 
    while ($user_posts->have_posts()) {
        $user_posts->the_post();

The above code while seems correct does not work. When I use this code, it ignores the Post Status. Say I submit the form, from url "https://milyin.com/draft/" and the Input Field has text "Elon Musk" in it. The AJAX should do WP_Query and return, posts from current user, that have the word "Elon Musk" in them and also only show the "draft" posts. unfortunately It filters and shows Elon Musk posts from all Post Statuses.

My Initial suspicion was that $_POST['url'] is undefined or wrongly defined. But then I randomly decided to remove the search parameter. That is shown below.

$args = array(
    'post_type'     => array('post', 'download', 'page'),
    'post_status'   => array('draft'),
    'author'        => $Current_Id,
    'numberposts'   => -1,
    'orderby'           => 'post_date',
    'order'             => 'DESC',
    'posts_per_page'    => 20,
    'paged'      => $_POST['pagenum'], 
 );

As U can see the only thing changed in the above code is that the line 's' => $_POST['search'], is missing. And when I submitted the form this time. It showed only the draft posts as I expected. Then I tried the opposite.

I tried, to remove 's' => $_POST['search'], and include 'post_status' => $post_status, and boom it works again. It was just showing all draft posts as I expect it to do.

So In Nutshell, My code runs perfect, if I just Query post_status, it also runs perfect when I just query s but it does not run when I s and post_status both together. Key Things to remember, this code is running using AJAX, the $_POST['url'] seems obvious suspect, but it is running perfectly in my opinion, and I am using this code to echo (not returning any thing) Title Featured Image etc.

When I run the query with s and post_status together, it just shows search results from All Post_type, also, when I run this code directly without AJAX, then also it works.

3
  • 1
    Can you show the results of print_r($user_posts); ? – Eduardo Escobar Sep 21 '20 at 19:53
  • Sure. Give me few mins and I will update question with it. – Aditya Agarwal Sep 22 '20 at 10:44
  • I did try echoing what you said, due to security reasons, and not telling my Database Table names and other info, I shall not share the output, but definitely in the input we do have the array for post_status in correct way – Aditya Agarwal Sep 22 '20 at 17:17
1

Below you will find some things you might want to consider regarding post types. But one note up front:

Every WP_Query is done for the current user. If you do not see what you expect, try it with a logged in admin user first. This way you can easily check if the problem is a capability issue.

About the post status you seem to be querying from the DB:

  • private: This post status is only available for Administrators who are logged in.
  • publish: Double check if you got these in the results. They are there by default. You don't even need to add them.
  • privatised: Is this a custom post status? If so, make sure you don't have set exclude_from_search set. Also note, that custom status are not supported as per now.
  • draft: iirc they are only visible if you have the edit_others_posts capability available for the role that the searching user has.
  • inherit: This will include attachments and revisions. Note that this might be a capability issue as well.
  • future: Same as above.

Make sure to test the "problem" you are facing via a mini-plugin in some admin screen first to check if it's an admin UI/ frontend problem. Constants are different, etc. If it works there, you have either missing constants or a user role that is not capable of viewing what you want to see in the queried results.

Make sure to read @TomJNowells answers about taxonomies vs other things like post types. TL;DR: You will find out that in some cases you do not want a taxonomy … or want nothing than a taxonomy. Also it's not that hard to register a custom taxonomy with custom capability handling and maybe a custom meta box. This way you can navigate around any problems that are related to the Tumblr-Clone Feature Post Status, that was never finished and never properly thought trough and never was fully implemented. It might be much easier to get there by doing what you want to do without any restrictions from an obsolete and left over feature.

Links to developer.wordpress.org:

5
  • Thanks, but it doesn't solve purpose. I was checking it when the current logged in user was the WordPress Admin, and it doesn't work. There surely is some issue with s parameter and draft posts – Aditya Agarwal Sep 22 '20 at 10:42
  • Also, yes the Privatised is custom post type, and I set author in args because, even if I am admin I just want to see my own posts only. – Aditya Agarwal Sep 22 '20 at 14:18
  • @AdityaAgarwal privatised is listed as post_status, not as post type in the code you showed in your question. Also you did not answer if you tested that it works in the admin area like described in the answer. – kaiser Sep 23 '20 at 21:10
  • oh yes, I tried it in admin area too, not working. And my bad its custom post type not custom post status. I tried doing echo $user-posts->request, to echo the SQL of the WP_Query, and god knows why the SQL output does not contain the post status in the SQL "WHERE" – Aditya Agarwal Sep 24 '20 at 10:35
  • @AdityaAgarwal Search my answers for debugging SQL output here on this site. That should help. – kaiser Sep 29 '20 at 10:17

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.