Support » Plugins » Hacks » Customizing Search Queries

  • Resolved pramathesh



    I am trying to modify the default search results in my site.

    There is a category called news.

    When I am searching while from the news archive page or a news post then it should show results only from news category.

    When I am searching from rest of the site, it should show results excluding the news. I am using a flag to detect the source page to detect if it was a news page or not. If it is a news page then the flag $is news would become 1 and if not it would be 0. But, somehow it is not setting the value and it is taking the default value 0.

    Setting the flag is done by a function call

    This is the following code in functions.php

    global $is_news;
    function set_is_news($value) {
    	global $is_news;
    function exclude_category( $query ) {
    // The category_id is 1 for news
    	global $is_news;
    	if ( $query->is_home) { $is_news=0;}
        if ( $query->is_search ) {
    	if ($is_news==1)
    		$query->set( 'cat', '1' );
    		$query->set('cat', '-1' );
    		$query->set('post_type', 'post');
Viewing 1 replies (of 1 total)
  • Moderator bcworkz


    Global values are only valid for the current request. Once a search is requested, it becomes a new request and all globals are reset. There’s a few ways to deal with this, the most straight forward may be to pass a value as an additional URL parameter to the search form’s GET request and modify your query based on it’s presence. Or pass a value in a hidden form field if your form is using the POST method.

Viewing 1 replies (of 1 total)
  • The topic ‘Customizing Search Queries’ is closed to new replies.