Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1405262

    For an artist page i had to setup a lot of pages with some text and masonry to show a bunch of images.
    Not all Images got their own post – but to my own surprise, image titles, captions, descriptions are not part of the search at all.
    The images are then not found despite searching for a term that belongs to the attachment.

    there was a snippet here on the forum that includes custom post types (cpt) to search. So i thought that might be possible for attachment too.
    and here we go ( you can add to the array of post_type your CPT or Products or Event etc. – you had to list all post_types you like to include ):
    this to child-theme functions.php:

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

    on ajax search there will be then a media section:

    now. if you like to change the size for the images shown on the attachment page use
    again: to child-theme functions.php:

    function custom_prepend_attachment( $attachment_content ){      
            // set the attachment image size to 'large' or even avia images-sizes work here f.e. masonry
            $attachment_content = sprintf( '<p class="attachment-image">%s</p>', wp_get_attachment_link(0, 'masonry', false) );  
            // return the attachment content
            return $attachment_content;      
    }
    add_filter( 'prepend_attachment', 'custom_prepend_attachment' );

    next the search results link to the attachment page. – but this isn’t realy informative – so if you like to add f.e. the caption and description to that page use this too in your child-theme functions.php:

    function my_attachment_description($caption_text) {
    	global $post;
    	$a_post = get_post();
    	$caption_text .= $a_post->post_excerpt ? ( '<p class="wp-caption-text">'. wptexturize( $a_post->post_excerpt ) . '</p>' ) : '';
    	return $caption_text;
    }
    add_filter('prepend_attachment', 'my_attachment_description');

    the rest will be a bit styling ( it is easy now with the classes set above: attachment-image and wp-caption-text )

    #1405317

    Hey Guenter,
    Thanks for sharing Guenni007 we will leave this open should anyone has questions about your code.

    Best regards,
    Mike

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.