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 "/" 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.
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/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.
1 Answer
Reset to default 1Below 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 setexclude_from_search
set. Also note, that custom status are not supported as per now.draft
: iirc they are only visible if you have theedit_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
:
- Search
s
parameter - Status
post_status <string|array>
parameters
print_r($user_posts);
? – Eduardo Escobar Commented Sep 21, 2020 at 19:53