• As you know, media pages (attachments) are excluded by the developers of the site’s life – they are not displayed in the search, comments on them are not displayed in the last comments block. How to remove these restrictions?

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @sdax

    In order to “unlock” this functionality, you’re probably going to have to hook into one or more of the ‘pre_get_’ hooks in a theme or child theme’s functions.php file, or a custom plugin, to allow different data types.

    For example, for a search, I found this code which uses the pre_get_posts hook to include attachments in search results.

    add_filter( 'pre_get_posts', 'pre_get_attachment_search' );
    function pre_get_attachment_search( $query ) {
    	if ( $query->is_search ) {
    		$query->set( 'post_type', array( 'post', 'attachment' ) );
    		$query->set( 'post_status', array( 'publish', 'inherit' ) );
    	}
    	return $query;
    }

    For the comments, you would probably need to use the pre_get_comments hook, but without diving into the code I’m not sure what you’d need to set on the $query to include attachment comments.

    Thread Starter sdax

    (@sdax)

    I used the code you provided, cleared the cache, but no result, unfortunately.

    @sdax I tested the code for the search on my local development environment before sharing it with you, it did work as expected. (See the screenshot of the two images in my media library in the search results https://tinyurl.com/22996kjp).

    Can I check with you, how you have enabled the code in your site, and are you using the standard WordPress search function?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to unlock media pages (attachments)?’ is closed to new replies.