{"id":2198,"date":"2019-05-01T16:04:41","date_gmt":"2019-05-01T23:04:41","guid":{"rendered":"https:\/\/codecharismatic.com\/?p=2198"},"modified":"2019-05-02T16:00:30","modified_gmt":"2019-05-02T23:00:30","slug":"relevanssi-how-to-exclude-woocommerce-product-variations","status":"publish","type":"post","link":"https:\/\/codecharismatic.com\/relevanssi-how-to-exclude-woocommerce-product-variations\/","title":{"rendered":"Relevanssi: How to Exclude WooCommerce Product Variations From WordPress Search Results"},"content":{"rendered":"

By default, Relevanssi doesn’t un-include<\/a> search results that are draft, pending, private, etc. This is problematic since you may not want products to be available via search.<\/p>\n

This solution takes a product variation’s parent post status into account. If the variation’s parent in question is anything but ‘publish’ it de-indexes the product variation.<\/p>\n

Add it to your functions.php and rebuild your index.<\/p>\n

\r\nadd_filter( 'relevanssi_do_not_index', 'relevanssi_search_do_not_index', 10, 2 );\r\n\r\nfunction relevanssi_search_do_not_index( $exclude, $post_id ) {\r\n\r\n\t\/\/ get post\r\n\t$post = get_post( $post_id );\r\n\r\n\t\/\/ exclude if post is variation and it's parent post status is not publish OR posts status is not publish\r\n\tif( ( $post->post_type == 'product_variation' && get_post_status( $post->post_parent )  != 'publish' ) || $post->post_status != 'publish' ) {\r\n\r\n\t\t$exclude = true;\r\n\r\n\t} \r\n\r\n\treturn $exclude;\r\n\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

By default, Relevanssi doesn’t un-include search results that are draft, pending, private, etc. This is problematic since you may not want products to be available via search. This solution takes a product variation’s parent post status into account. If the…<\/span><\/p>\n

Read more ›<\/a><\/div>\n

<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[131,132,109,51],"_links":{"self":[{"href":"https:\/\/codecharismatic.com\/wp-json\/wp\/v2\/posts\/2198"}],"collection":[{"href":"https:\/\/codecharismatic.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codecharismatic.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codecharismatic.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/codecharismatic.com\/wp-json\/wp\/v2\/comments?post=2198"}],"version-history":[{"count":20,"href":"https:\/\/codecharismatic.com\/wp-json\/wp\/v2\/posts\/2198\/revisions"}],"predecessor-version":[{"id":2219,"href":"https:\/\/codecharismatic.com\/wp-json\/wp\/v2\/posts\/2198\/revisions\/2219"}],"wp:attachment":[{"href":"https:\/\/codecharismatic.com\/wp-json\/wp\/v2\/media?parent=2198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codecharismatic.com\/wp-json\/wp\/v2\/categories?post=2198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codecharismatic.com\/wp-json\/wp\/v2\/tags?post=2198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}