This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Media Search Results

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #38054
    mattlecountmattlecount
    Participant

    Hi,

    We are querying some functionality to see if possible with settings, or could be added.

    For media files, we only want to return items that are attached to posts, in this case a custom post type ‘candidate’
    We’d also like to return not the file name itself in the result, but the title of the post it is attached to. Is this possible?

    #38064
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    I think both should be possible via a custom code snippet.

    Try adding this code to the functions.php file in your theme/child theme directory – make sure to have a full server back-up first for safety. For more details you can check the safe coding guidelines.

    add_filter( 'asp_results', 'asp_attachment_title_field', 10, 1 );
    function asp_attachment_content_field( $results ) {
      foreach ($results as $k=>&$r) {
    	if ( $r->content_type == 'attachment' ) {
    		$parent_id = wp_get_post_parent_id( $r->id );
    		if ( !is_wp_error($parent_id) && !empty($parent_id) ) {
    			$parent = get_post( $parent_id );
    			if ( $parent->post_type == 'candidate' ) {
    				// Change the title
    				$r->title = get_the_title( $parent);
    			} else {
    				unset($results[$k]);
    			}			
    		}
    	}
      } 
      return $results;
    }

    This should change the titles to the parent post title, and remove items from the results where the post type is not “candidate”

    #38078
    mattlecountmattlecount
    Participant

    This seemed to prevent the search from work all together. We are using the index table as we’re searching PDF contents if this would affect that?

    #38084
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    No, the engine type has no effect on this snippet. It must be something else. I don’t see any errors in the code though. Do you see any error message instead of the results, or does it just says, that there are no results at all?

    #38098
    mattlecountmattlecount
    Participant

    Hi,

    Just no results at all.

    #38102
    Ernest MarcinkoErnest Marcinko
    Keymaster

    That could only mean 3 things, either the media files are not actually attached to the parent post, or the post type is incorrect – or there is something wrong with the code.

    I don’t see any obvious errors there, so I may need to check this directly. Can you please add temporary FTP and back-end access? Thank you!

    #38107
    mattlecountmattlecount
    Participant

    You cannot access this content.

    #38121
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Thank you!

    It was luckily a minor error in the code, that I didn’t notice, with some testing I was able to see it right away. I placed the corrected function there, for future reference this is the correct code:

    add_filter( 'asp_results', 'asp_attachment_title_field', 10, 1 );
    function asp_attachment_title_field( $results ) {
      foreach ($results as $k=>&$r) {
    	if ( $r->content_type == 'attachment' ) {
    		$parent_id = wp_get_post_parent_id( $r->id );
    		if ( !is_wp_error($parent_id) && !empty($parent_id) ) {
    			$parent = get_post( $parent_id );
    			if ( $parent->post_type == 'candidate' ) {
    				// Change the title
    				$r->title = get_the_title( $parent);
    			} else {
    				unset($results[$k]);
    			}			
    		}
    	}
      } 
      return $results;
    }
    #38122
    mattlecountmattlecount
    Participant

    That’s, great. Thanks so much for the support!

    #38123
    Ernest MarcinkoErnest Marcinko
    Keymaster

    You cannot access this content.

Viewing 10 posts - 1 through 10 (of 10 total)
  • The topic ‘Media Search Results’ is closed to new replies.