Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Media Search Results
This topic contains 9 replies, has 2 voices, and was last updated by Ernest Marcinko 1 year, 10 months ago.
- AuthorPosts
- June 15, 2022 at 2:31 pm #38054
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?June 16, 2022 at 3:12 pm #38064Hi,
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 :)
June 17, 2022 at 2:22 pm #38078This 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?
June 18, 2022 at 10:12 am #38084Hi,
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 :)
June 20, 2022 at 10:18 am #38098Hi,
Just no results at all.
June 20, 2022 at 11:55 am #38102That 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 :)
June 20, 2022 at 3:39 pm #38107You cannot access this content.June 21, 2022 at 1:35 pm #38121Thank 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:
Best,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; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
June 21, 2022 at 1:43 pm #38122That’s, great. Thanks so much for the support!
June 21, 2022 at 3:34 pm #38123You cannot access this content. Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
- AuthorPosts
The topic ‘Media Search Results’ is closed to new replies.