Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Media Search Results
- This topic has 9 replies, 2 voices, and was last updated 3 years, 11 months ago by
Ernest Marcinko.
-
AuthorPosts
-
June 15, 2022 at 2:31 pm #38054
mattlecount
ParticipantHi,
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 #38064Ernest Marcinko
KeymasterHi,
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”
June 17, 2022 at 2:22 pm #38078mattlecount
ParticipantThis 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 #38084Ernest Marcinko
KeymasterHi,
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?
June 20, 2022 at 10:18 am #38098mattlecount
ParticipantHi,
Just no results at all.
June 20, 2022 at 11:55 am #38102Ernest Marcinko
KeymasterThat 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!
June 20, 2022 at 3:39 pm #38107mattlecount
ParticipantYou cannot access this content.
June 21, 2022 at 1:35 pm #38121Ernest Marcinko
KeymasterThank 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; }June 21, 2022 at 1:43 pm #38122mattlecount
ParticipantThat’s, great. Thanks so much for the support!
June 21, 2022 at 3:34 pm #38123Ernest Marcinko
KeymasterYou cannot access this content.
-
AuthorPosts
- The topic ‘Media Search Results’ is closed to new replies.