• Is there a way to exclude attachment pages from the search results if they are marked as ‘unattached’ in the WordPress Media Library?

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

    (@msaari)

    Yes, just add this to your theme functions.php and reindex:

    add_filter( 'relevanssi_indexing_restriction', 'rlv_exclude_unattached' );
    function rlv_exclude_unattached( $restriction ) {
    	global $wpdb;
    	$restriction['mysql']  .= " AND post.ID NOT IN (SELECT ID FROM $wpdb->post WHERE post_type = 'attachment' AND post_parent = 0) ";
    	$restriction['reason'] .= ' Not attached to a post.';
    	return $restriction;
    }
    Thread Starter suntower

    (@suntower)

    Thanks. It throws numerous errors. Is this feature new? UNFORTUNATELY, I have a site still using Relevanssi 4.2.3. But it also looks like an interaction with Yoast. Ideas?

    Notice: Undefined property: wpdb::$post in /home/content/55/8933555/html/jcharrisfordesmoines/wp-includes/wp-db.php on line 648
    
    Notice: Undefined index: mysql in /home/content/55/8933555/html/jcharrisfordesmoines/wp-content/themes/jchwebdev-twentysixteen/functions.php on line 1441
    
    Notice: Undefined index: reason in /home/content/55/8933555/html/jcharrisfordesmoines/wp-content/themes/jchwebdev-twentysixteen/functions.php on line 1442
    
    Notice: Array to string conversion in /home/content/55/8933555/html/jcharrisfordesmoines/wp-content/plugins/relevanssi/lib/compatibility/yoast-seo.php on line 44
    
    WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Array AND post.ID NOT IN (SELECT post_id FROM wp_postmeta WHERE meta_key = '_yoa' at line 1]
    SELECT ID FROM wp_posts AS post WHERE ID = 3547 Array AND post.ID NOT IN (SELECT post_id FROM wp_postmeta WHERE meta_key = '_yoast_wpseo_meta-robots-noindex' AND meta_value = '1' )
    Plugin Author Mikko Saari

    (@msaari)

    I thought it was old, but yes, it was added in 4.7.1. Why are you using such an ancient version of Relevanssi? Anyway, in the old version, the function needs to look like this:

    add_filter( 'relevanssi_indexing_restriction', 'rlv_exclude_unattached' );
    function rlv_exclude_unattached( $restriction ) {
        global $wpdb;
        return " AND post.ID NOT IN (SELECT ID FROM $wpdb->post WHERE post_type = 'attachment' AND post_parent = 0) ";
    }

    This should work better (but will break things if you update Relevanssi to version 4.7.0 or later).

    Thread Starter suntower

    (@suntower)

    Tried it. It does not throw an error, but it does nothing.

    Any ideas?

    Plugin Author Mikko Saari

    (@msaari)

    Well, I don’t know. Could well be some bug in the code; I can’t really support a version this old, there have been lots of releases and lots of bugs removed after that, and I’m not terribly interested in trying to guess which old bug might be causing this not to work. I’ve fixed those bugs already, so please just upgrade to the latest version.

    Thread Starter suntower

    (@suntower)

    OK then…

    Can you tell me how to output the complete SQL query that is being run on the search page -after- it is processed by this hook so I can see what is happening for myself?

    Thanks in advance.

    Thread Starter suntower

    (@suntower)

    Also: note that the filter you gave me is running when I hit Update on editing a post. I guess I had expected it to hook into the actual Search Query.

    Perhaps there is another filter I could apply this to?

    Thanks in advance.

    Thread Starter suntower

    (@suntower)

    Sorry… I’ll stop bugging you with this one last thing. It appears that the code you gave me prevents the index from being built. It wipes out the index, the gets stuck during the Counting…

    Is there a way to debug what is getting stuck? SQL query being run?

    Thanks as always.

    Plugin Author Mikko Saari

    (@msaari)

    That is indeed an indexing filter that will only affect the indexing process. It’s possible to filter this in searching as well, but it’s much more effective when done in indexing, because there’s no point keeping things in index that are not wanted in the search.

    I noticed there’s a type in the code I gave you. For 4.3.2, it should be:

    add_filter( 'relevanssi_indexing_restriction', 'rlv_exclude_unattached' );
    function rlv_exclude_unattached( $restriction ) {
        global $wpdb;
        return " AND post.ID NOT IN (SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = 0) ";
    }

    and from 4.7.1 onwards:

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

    These should work better.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Exclude unattached media from search results?’ is closed to new replies.