• Hi Mikko,

    I’m using your plugin and am very happy with it πŸ™‚ Thanks for the great work!

    I do have a question about attachments. I’m including the attachments in the search results as I need those in order to show products where specific queries are present in their file names only, but not in the product title or product descriptions.

    However, I have some blog posts on the site as well and they contain images as attachments. Those images appear in the search results as well, but I would like to show only results related to products. I don’t include the posts in the search results.

    Is there a way to exclude attachments from wordpress posts from the search results, but keep the attachments related to woocommerce products?

    Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Did you notice there’s a setting in Relevanssi to exclude image attachments from the index? Would that solve this problem?

    If not, then you can add an indexing filter that excludes attachments that aren’t attached to products. The filter hook for that is relevanssi_indexing_restriction.

    add_filter( 'relevanssi_indexing_restriction', function( $restriction ) {
    	global $wpdb;
    	$restriction['mysql']  .= " AND post.ID NOT IN (
          SELECT post_id FROM $wpdb->posts
          WHERE post_type = 'attachment' AND post_parent NOT IN ( SELECT post_id FROM $wpdb->posts WHERE post_type = 'product' ) ) ";
    	$restriction['reason'] .= ' Non-product attachment';
    	return $restriction;
    } );

    This should exclude the non-product attachments. I haven’t tried this, so it’s possible I’ve made a mistake in the MySQL, but this is the general idea, at least.

    Thread Starter TheGreyRabbit

    (@ayo-1)

    Hi Mikko,

    thanks for your reply!

    I added your code to my functions.php, but it creates a fatal error on the frontend.

    Plugin Author Mikko Saari

    (@msaari)

    Ah, I forgot it’s not possible to use anonymous functions on that hook. This should work better:

    add_filter( 'relevanssi_indexing_restriction', 'non_product_attachments' );
    function non_product_attachments( $restriction ) {
    	global $wpdb;
    	$restriction['mysql']  .= " AND post.ID NOT IN (
          SELECT post_id FROM $wpdb->posts
          WHERE post_type = 'attachment' AND post_parent NOT IN ( SELECT post_id FROM $wpdb->posts WHERE post_type = 'product' ) ) ";
    	$restriction['reason'] .= ' Non-product attachment';
    	return $restriction;
    }
    Thread Starter TheGreyRabbit

    (@ayo-1)

    When I add that snippet and click the button to rebuild the index, it shows 0 posts found and the process ends with 0.

    Plugin Author Mikko Saari

    (@msaari)

    Well, that falls under the “I haven’t tested it”… At least I spotted some typos:

    add_filter( 'relevanssi_indexing_restriction', 'non_product_attachments' );
    function non_product_attachments( $restriction ) {
    	global $wpdb;
    	$restriction['mysql']  .= " AND post.ID NOT IN (
          SELECT ID FROM $wpdb->posts
          WHERE post_type = 'attachment' AND post_parent NOT IN ( SELECT ID FROM $wpdb->posts WHERE post_type = 'product' ) ) ";
    	$restriction['reason'] .= ' Non-product attachment';
    	return $restriction;
    }

    Other than that, the logic seems to be ok.

    Thread Starter TheGreyRabbit

    (@ayo-1)

    That last snippet works! Thanks a lot for your help!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Excluding post attachments from search results’ is closed to new replies.