#StackBounty: #php #posts #wp-query #functions posts_per_page displays only 2 posts instead of 4 posts

Bounty: 50

I have a wordpress/php code as shown below in which I am trying to pull latest 4 channels at Line A in the code below.

function get_latest_channels( $instance_id = false ) {
    echo $instance_id;
    echo "<br>";
    if ( ! $instance_id ) {
        $query_args = array(
                'post_type'      => 'hello-channels',
                'ep_integrate'   => true,
                'posts_per_page' => 4,
        );
    } else {
        $query_args = array(
                'post_type'      => 'hello-channels',
                'ep_integrate'   => true,
                'meta_key'       => 'instance_id',
                'meta_value'     => $instance_id,
                'posts_per_page' => 4,
        );
    }
    $channels = new WP_Query( $query_args );
    if ( $channels->have_posts() ) {
        $rtn = $channels->posts;
    }

    return $rtn;
}

$instance_id = 49;
$latest_channels = HELLOChannelsget_latest_channels($instance_id);
echo "<pre>"; print_r($latest_channels); echo "</pre>". // Line A

The value of $instance_id is 49. On using 49 value, it should pull 4 latest channels.

Problem Statement:

My code is working fine dev/staging server (meaning its pulling latest 4 channels at Line A) but in production server its pulling only 2 channels.

I am wondering what changes I need to make in the php code above so that it pulls latest 4 channels in the production server.

If I comment this line 'posts_per_page' => 4, in the else section of the function get_latest_channels( $instance_id = false ) then Line A is pulling 30 posts in dev server.


Get this bounty!!!

#StackBounty: #php #wp-query #ajax #search #query-posts Can't search posts using WP_QUERY inside AJAX Function

Bounty: 50

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.


Get this bounty!!!

#StackBounty: #php #wp-query #ajax #search #query-posts Can't search posts using WP_QUERY inside AJAX Function

Bounty: 50

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.


Get this bounty!!!

#StackBounty: #php #wp-query #ajax #search #query-posts Can't search posts using WP_QUERY inside AJAX Function

Bounty: 50

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.


Get this bounty!!!

#StackBounty: #php #wp-query #ajax #search #query-posts Can't search posts using WP_QUERY inside AJAX Function

Bounty: 50

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.


Get this bounty!!!

#StackBounty: #php #wp-query #ajax #search #query-posts Can't search posts using WP_QUERY inside AJAX Function

Bounty: 50

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.


Get this bounty!!!

#StackBounty: #php #wp-query #ajax #search #query-posts Can't search posts using WP_QUERY inside AJAX Function

Bounty: 50

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.


Get this bounty!!!

#StackBounty: #php #wp-query #ajax #search #query-posts Can't search posts using WP_QUERY inside AJAX Function

Bounty: 50

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.


Get this bounty!!!

#StackBounty: #php #wp-query #ajax #search #query-posts Can't search posts using WP_QUERY inside AJAX Function

Bounty: 50

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.


Get this bounty!!!

#StackBounty: #php #wp-query #ajax #search #query-posts Can't search posts using WP_QUERY inside AJAX Function

Bounty: 50

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.


Get this bounty!!!