2

I am adding a default WordPress Search widget through Elementor on two of my page, page X and page Y. Page X ID = 100, page Y ID = 200. I want the user to be able to search through category 37 when he is on page X, and be able to search through category 24 when he is on page Y. I wrote this code:

function searchcategory($query) {
    if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
        if ( is_page(100) ) {
            $query->set('cat',37);
        }
        else if ( is_page(200) ) {
            $query->set('cat',24);
        }
    }
}

add_filter('pre_get_posts','searchcategory');

However, it does not work properly. It returns pages which have different categories and IDs etc. Also, results are same on both page X and page Y. Can anyone help editing the code?

Note: the code below works fine though:

function searchtest($query) {
    if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
        $query->set( 'cat', 39 );
    }
}
add_action( 'pre_get_posts', 'searchtest' );
  • That's not gonna work. If you're on a search page, you are neither on page 100 nor page 200. You are on search page. – Jack Johansson Jun 14 '18 at 7:17
  • Yeah. My question should have been: if a user initiates a search on page with ID = 100, he should be transferred to the page with search results, which will only show posts with category 39. So I was doing everything wrong then – tera_789 Jun 14 '18 at 18:00
1

The answer before mine shows you the problem about is_search() in your code.

To solve your problem, you can try to add some datas from your search form. In your WordPress you have a searchform.php, you can edit this file to add a new hidden field or use an ugly filter function like I have do here :

// Gives you the category where you want to search with from page ID
add_filter('wpse_306057_search_category_id', 'wpse_306057_search_category_id', 10, 1);
function wpse_306057_search_category_id($id = false) {
    switch($id)
    {
        case 100:
        $cat_id = 37;
        break;

        case 200:
        $cat_id = 24;
        break;


        case 201:
        case 202:
        case 203:
        $cat_id = array(57,99); // You may use multiple cats
        break;


        default:
        $cat_id = false;
        break;
    }
    return $cat_id;
}

// Add input hidden with "from page" for your search form
add_filter('get_search_form', 'wpse_306057_search_category_input', 10, 1);
function wpse_306057_search_category_input($form) {
    return str_replace('</form>', '<input type="hidden" name="search_from_page" value="'.get_queried_object_id().'" /></form>', $form);
}

// Add cat to your query
add_filter('pre_get_posts', 'wpse_306057_search_category', 10, 1);
function wpse_306057_search_category($query) {
    if(!is_admin()
    && $query->is_main_query()
    && $query->is_search()
    && !empty(@$_GET['search_from_page'])
    && apply_filters('wpse_306057_search_category_id', $_GET['search_from_page']))
    {
        $query->set('cat', apply_filters('wpse_306057_search_category_id', $_GET['search_from_page']));
    }
}

I haven't tested the code, but it's a good way to play with what you want to achieve.

0

Well, let's try to understand what is your code doing...

In this line:

if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) { 

you check if you're not in admin area, if the query is the main query for the current page and if the current page is a search page.

And here:

if ( is_page(100) ) {

you check if the current page is page with ID 100.

So what's the problem? These conditions won't be satisfied both... It's either a page or a search results page...

  • Thanks again! So is_search() function represents my search page? What exactly do you mean by search page? I have a default WordPress search widget inserted through Elementor on two of my pages. – tera_789 Jun 14 '18 at 6:27
  • I mean the page that WordPress uses for displaying search results... – Krzysiek Dróżdż Jun 14 '18 at 6:28
  • Oh I see. But yeah then, my question should have been: if a user initiates a search on page with ID = 100, he should be transferred to the page with search results, which will only show posts with category 39. So I was doing everything wrong then – tera_789 Jun 14 '18 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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