Media Search Results

This topic contains 9 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 1 year, 10 months ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #38054
    mattlecount
    mattlecount
    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 Marcinko
    Ernest 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”

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #38078
    mattlecount
    mattlecount
    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 Marcinko
    Ernest 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?

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #38098
    mattlecount
    mattlecount
    Participant

    Hi,

    Just no results at all.

    #38102
    Ernest Marcinko
    Ernest 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!

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #38107
    mattlecount
    mattlecount
    Participant
    You cannot access this content.
    #38121
    Ernest Marcinko
    Ernest 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;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #38122
    mattlecount
    mattlecount
    Participant

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

    #38123
    Ernest Marcinko
    Ernest Marcinko
    Keymaster
    You cannot access this content. Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘Media Search Results’ is closed to new replies.