Search only postmeta
-
Hi, you can tell me if he’s only going to look after the postmeta field? Ex. key _product_content. If I am looking after the post_content field, I find products like 1080 for example
-
Yes, it’s possible. You need to create your own custom indexing hook, which could look like this
add_filter('wpfts_index_post', function($index, $post) { global $wpdb; // You can specify this rule for your specific post_type if ($post->post_type == 'product') { // Adding new domain "product_content" specially for posts of this post_type // You need to keep in mind, the "product_content" is just an arbitrary domain name here, you can use any $index['product_content'] = get_post_meta($post->ID, '_product_content', true); } return $index; }, 3, 2);
Ok, fine. After that, you need to make a Rebuild Index. Then you can add some weights for your domains. In the list of domains you will see “post_title”, “post_content” and your new domain “product_content”. If you want to search by product_content only, just put weight 1 for it, and 0 for others. And that’s it.
Thanks for your question and let me know if it is resolved for you.
Thanks for the quick reactions, but something is not working or we have not understood. Becouse _product_content I keep the postmeta table as meta_key value and I would like to filter only after this field. My code is:
add_filter('wpfts_index_post', function($index, $post) { global $wpdb; if ($post->post_type == 'product') { $index['product_content'] = get_post_meta($post->ID, '_product_content', true); } return $index; }, 3, 2);
Hi, please make sure your target posts have the post_type = ‘product’. I took this value from the mind, but you maybe need to change this to yours.
Also please check if you have the “product_content” cluster added to WPFTS Settings / Main Configuration / Search and Relevance Settings / Cluster Weights
https://e-wm.org/i/E20190801-205333-001.pngIf you can see the cluster there, it means data was indexed correctly.
Also, you can test your wpfts_index_post function with Sandbox Area. Enter the POST ID to the input above and you will see the list of cluster data in the results.
As he tests in a sandbox, he returns a wrong result. When entering in Search Tester “1080” returns the wrong product. 1080 is part of the class name from Guttenberg. Therefore, it copies and saves the title + content in the postmeta under the key _product_content. This is my configuration https://ibb.co/m9FsS8w.
I made a code for tests:
add_filter('wpfts_index_post', function($index, $post) { global $wpdb; $index['product_content'] = get_post_meta($post->ID, '_product_content', true); return $index; }, 3, 2);
But it also shows the wrong result ??
Could I see the result of your Sandbox Area output for this product, please?
I mean the result when you enter your post_id to the top input and press Test Filter
https://e-wm.org/i/E20190801-214950-001.pngYou may know the post_id using the URL in the admin area, for example if you edit the product in woocommerce, the link looks like this
https://domain.com/wp-admin/post.php?post=700&action=editso the post_id=700 here.
- This reply was modified 5 years, 3 months ago by Epsiloncool.
I enter it in: https://ibb.co/HN3nXHx . And I would like it to work in the main search engine as well.
Please explain why you are looking for “1080”? Is this post_id or the query string which you are looking for?
Try to enter 1080 to the TOP INPUT, which reads “Post ID” and press top button.How the _product_content meta_value looks? Please share it.
_product_content is a combination of fields: title and content from the posts table only without HTML, which Guttenberg gives me. 1080 is not post_id only part of the css class that Guttenberg has given me.
Ok, let’s go step by step
1. Please find the actual post_id for the record you’re trying to search for (which post_meta you need to index).
2. Put this post_id to the Sandbox Area (TOP INPUT) and see the result.
What do you see?
https://ibb.co/MB7dy41. ID is looking good. Additionally, it shows _product_content correctly. But I do not want to enter post_id just a phrase or title.
Alright! So 1070 is your post ID.
And it looks like your wpfts_post_index function works correctly.
Now the WPFTS should include your product_content data to the Search Index and thus this post will be searchable by the product_content text.
3. Please make the Rebuild Index now (the button at the Main Configuration tab)
4. Wait till the rebuild complete
5. Go to the site and enter “podgrzewacz” (or different word from product_content) in the search widget. The search result should contain the post 1070.
You also can enter the word “podgrzewacz” to the bottom input in the Sandbox Area, you should get the same result.
Yes, it worked before the problem is that if I type “1080” – which is a class from Gutenberg, it also also finds me “heater”, and he is not in the title or content “1080”. I do not know if you understand what I mean? I would like to block the search after the table “posts”, and look only after “postsmeta.” Is there an easy way to do this?
- The topic ‘Search only postmeta’ is closed to new replies.