• Hi WordPress community, I am making a book store website using elementor. I am stuck at a problem where I have a search bar for products. However, when I enter the search term, it only shows the books with that name.

    What I need is if I enter the author name, it should show all the books by that author as well. I am unable to do the same. Can someone please help me with it?

    I have shared the link to the page I need help with. On the header, you will find the search icon. Click on it and search for the term “Flamingo” and you will see the search results. However, when you search for an author for example “Shamim Padamsee” you won’t get any results.

    Please let me know if I need to explain this further as well?

    • This topic was modified 3 weeks, 5 days ago by aashjain.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • You need a plugin that extends the WordPress search. Take a look at this one: https://www.ads-software.com/plugins/wp-extended-search/

    Hi @aashjain ,

    Thank you for reaching out to us in the www.ads-software.com forums.

    I understand you want your search function to include results for author names, not just book titles. By default, WordPress’s search only looks at post titles and content. So you’ll need to make some modification.

    First, make sure that the author names are stored in your product data. If you’re using WooCommerce, this might be in the product attributes or custom fields.

    Add some custom code to your theme to modify the search query. Add the following code to your theme’s functions.php file Or you can use You can use?Code Snippets?plugin and place this PHP code.

    function custom_search_query($query) {
    if ($query->is_search() && !is_admin() && $query->is_main_query()) {
    // Get the search term
    $search_term = $query->query_vars['s'];

    // Modify the query to also search in the author field
    $meta_query = array(
    'relation' => 'OR',
    array(
    'key' => '_book_author', // Change to your actual _book_author key
    'value' => $search_term,
    'compare' => 'LIKE'
    ),
    );

    $query->set('s', $search_term);
    $query->set('meta_query', $meta_query);
    }
    }

    add_action('pre_get_posts', 'custom_search_query');

    Make sure to replace '_book_author' with the actual key you’re using for authors in your product setup.

    Thread Starter aashjain

    (@aashjain)

    Hi Irfan,

    Thank you very much for the reply. By default, WooCommerce has a feature called Product author. Can we use that over here?

    I don’t want to get in to a situation where we already have a function and we are adding another one and it conflicts. Hope you Understand

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.