• Is there a way to display different search results, depending on the landing page (via PhP or plugin)?

    For example, if a user logs on to landing page A, if they do a search, products 1, 2, 3 are displayed (no matter what they are searching, do not display 4, 5, 6).

    If the user logs in to landing page B, then if they search, products 4, 5, 6 will be displayed (no matter what they search, 1, 2, 3 will not be displayed)

    Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Euler

    (@eulerarthur)

    Hey jesseguttenberg
    So, I found this article that presents some possible solutions, check it out https://www.wpbeginner.com/plugins/how-to-exclude-specific-pages-authors-and-more-from-wordpress-search/

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    I was going to suggest a function adding a pre_get_posts() filter, but found this and think it’s something you might be able to adapt: https://surniaulula.com/2020/apps/wordpress/plugins/woocommerce/a-better-pre_get_posts-search-for-woocommerce/

    I got there via https://www.google.com/search?q=pre_get_posts+on+search

    Thread Starter jesseguttenberg

    (@jesseguttenberg)

    Hey @sterndata, thanks for highlighting pre_get_posts() filter. The code in the link you shared was hard to follow so I tried searching the web and came up with another version shown below.

    I tried using the below code to exclude two product categories. However, it does not work in that it does not exclude the two product categories 43 and 46 which are the man and book product categories.

    Can you help me out, please?

    function main_search_form( $form ) {
        $form = '<form role="search" method="get" id="search-form" action="'. home_url( '/' ) .'">';
        $form .= '<label for="search">Search</label>';
        $form .= '<input name="s" id="search" size="15" type="text" value="Search" title="Search">';
    	$form .= '<input type="hidden" value="product" name="post_type" />';
    	$form .= '<input type="hidden" value="product_cat" name="man,book" />';	
        $form .= '<input type="submit" value="Submit">';
        $form .= '</form>';
    	echo "Test Form 3";
        return $form;
    }
    add_filter( 'get_search_form', 'main_search_form' );
    
    add_filter('pre_get_posts', 'exclude_cats_blog_search');
    function exclude_cats_blog_search($query){
    if ($query->is_search && $_GET['product_cat'] == 'man,book') {
    	$query->set('category__not_in', array (43,46));
    }}
    
    function wpc_elementor_shortcode22($atts) {
        return main_search_form($form);
    }
    add_shortcode( 'my_elementor_php_output3', 'wpc_elementor_shortcode22');
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Is this possible in wordpress?’ is closed to new replies.