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.
print_r($user_posts);
?