G
G
graf452312021-09-07 03:21:51
WordPress
graf45231, 2021-09-07 03:21:51

How to display products that contain the search word?

I received a task from an SEO specialist to make it possible to create a block with products by search query in the admin panel.

That is, he presses the create block button, enters a search query, and a block appears on the page with a set of products that match this query.
Everything is generally clear except for one thing - how to display products by search word? You need to search everywhere, starting from the title, category and tags, ending with the description and custom fields.

I studied a set of shortcodes in wookomers and did not find anything suitable.
The site currently uses the Search by Omega plugin for search. Maybe you can create blocks through it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Zolin, 2021-09-07
@graf45231

B wp_query()has an argument 's'to which you can pass a search query

$args = [
  'post_type' => 'product',
  's' => 'search phrase',
];

$query = new WP_Query( $args );

Search by default occurs in the fields post_title, post_excerpt, post_content. If you need to fix this behavior, look for, for example, wp_query search by post meta , there are solutions.
It remains to make a shortcode out of all this. Fortunately, WooCommerce is already full of product output shortcodes, you can copy any and edit it to suit your task.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question