I'm trying to use is_search to output some stuff from functions.php. I'm using is_search on the actual search page itself and it works fine:

if(is_search()) {
   echo get_search_query( '<h1>', '</h1>' );

But from functions.php this doesn't work:

function data_scroll_all(){
   if(is_search()) {
     $offset = $_POST['offset'];
     $category = $_POST['cat'];
     $args = array('posts_per_page' => 3, 'category__not_in' => 1, 'offset' => $offset, 'orderby' => 'date', 'cat' => $category);
     $loop = new WP_Query($args);
       if($loop->have_posts()) {
       get_template_part( 'content-archive' );
    } wp_reset_postdata();
 } else {
    echo "Nope!";


With this I just get "Nope!" when I'm on the search page.

I have my search template setup like this:

Template Name: Search Page
global $query_string;

$query_args = explode("&", $query_string);
$search_query = array();

foreach($query_args as $key => $string) {
    $query_split = explode("=", $string);
    $search_query[$query_split[0]] = urldecode($query_split[1]);
} // foreach

$search = new WP_Query($search_query);

I have a a waypoint "infinite-scroll" function that triggers when reaching the bottom of the page:

var ajax_url = $('.loadAll').attr('data-url');
var cat = $('.loadAll').attr('data-cat');
var offset = -3;
$('#infinite-load').waypoint(function (direction) {
    if (direction === 'down') {
        offset = parseInt(offset) + 3;
            dataType: "HTML",
            url: ajax_url,
            type: 'POST',
            data: {
                action: 'data_scroll_all',
                cat: cat,
                offset: offset
            beforeSend: function () {
                $(".loading").show(); //show image loading
            success: function (data) {
}, {
    offset: 'bottom-in-view'
  • 1
    How do you call data_scroll_all()? Oh, and read about parse_str(). ;)
    – fuxia
    Commented Jan 6, 2014 at 15:35
  • data_scroll_all is not actually used anywhere in the code you posted.
    – s_ha_dum
    Commented Jan 6, 2014 at 15:36
  • @s_ha_dum well yea, the call works fine as it actually echos "nope" where it's supposed to, are you saying that it may be a problem because I'm making an ajax call which is not search.php?
    – UzumakiDev
    Commented Jan 6, 2014 at 15:44
  • If you call the function too early, it cannot work. This why we are asking for actual usage. Without that, an answer is impossible. It would also help if you’d intend your code for readability.
    – fuxia
    Commented Jan 6, 2014 at 15:47
  • @toscho Ahh, I see, two ticks. Also, why did you mention parse_str()?
    – UzumakiDev
    Commented Jan 6, 2014 at 15:50

2 Answers 2


... the call works fine as it actually echos "nope" where it's supposed to, are you saying that it may be a problem because I'm making an ajax call which is not search.php?

An Ajax call is a new request to the server. The page that loads is (should be) wp-admin/admin-ajax.php which is template neutral. That is not a search page at all. is_search won't ever be true. What is_search does, in short, is check the main query to see if you are on a search results template page. With an AJAX call to admin-ajax.php, you aren't.

You could probably just remove that check as presumably your function is only used where appropriate, but you'd need to provide more context for a solid answer.

  • Great, that's exactly what I thought you meant when you inquired about the function. I need to check whether I am on the search page, I'm using a waypoint "infinite scroll" function that triggers "data_scroll_all" that trigger as you say is coming from admin-aja.php, so how else can I go about detecting what page I'm on so I can formulate an if() statement?
    – UzumakiDev
    Commented Jan 6, 2014 at 15:59
  • You can't detect what page you are on because you are on admin-ajax.php. If you need the function to operate based on the requesting "page" you will need to pass an argument through the request to admin-ajax.php
    – s_ha_dum
    Commented Jan 6, 2014 at 16:49
  • 2
    I just set a data attribute to 1 on the search page and just did a if(isset()). Thanks for the help :)
    – UzumakiDev
    Commented Jan 6, 2014 at 17:01

Your code doesn't show where you call data_scroll_all() from, so this is just a blind guess -- apologies if it misses the mark.

It shouldn't matter which .php fike the function is in, but is_search() can only be called from within The Loop -- if the place your function is called from is outside the loop, it won't work.

(It's checking whether the current query is a search query, not whether or not you're on a search page )

  • Hi, thanks for the reply :) you are correct, I've added some more code to my question.
    – UzumakiDev
    Commented Jan 6, 2014 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.