简体   繁体   中英

Adding Search (S) parameter to WP_Query gives Call to undefined function is_user_logged_in()

Im creating a AJAX feature that needs to search posts. Currently returning all like this works fine...

    $the_query = new WP_Query(
    array (
        'post_type'        => 'post',
        'posts_per_page'   => '10',
        'post_status'      => 'publish',
        'orderby'          => 'title',
        'order'            => 'ASC',
    )
);
foreach($the_query->posts as $post):
  echo $post->post_title;
endforeach;

However when i add search with s parameter like below...

    $the_query = new WP_Query(
    array (
        's'                => 'mysearch',
        'post_type'        => 'post',
        'posts_per_page'   => '10',
        'post_status'      => 'publish',
        'orderby'          => 'title',
        'order'            => 'ASC',
    )
);
foreach($the_query->posts as $post):
  echo $post->post_title;
endforeach;

I then get an error like

Fatal error: Call to undefined function is_user_logged_in() in /wordpress/wp-includes/query.php on line 2084

Which is the core WP files, im not sure im doing search correctly? Will i have to use a like sql statement to get this? Or am i not doing it correctly?! This is for displaying it front end (non logged in / logged in users)

Many thanks in advance!

I got the same problem.

`$args = array(

'post_type' => 'block',
'posts_per_page' => -1

);

$get_all_block_posts = new WP_Query($args);`

Then I tried:

` $args = array(

'post_type' => 'block',
'posts_per_page' => -1

);

$get_all_block_posts = get_posts($args);`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM