Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › PDF search results
- This topic has 10 replies, 2 voices, and was last updated 3 years, 5 months ago by
ballerinamoose.
-
AuthorPosts
-
January 1, 2023 at 12:03 am #40679
ballerinamoose
ParticipantHi,
I am having trouble with a few issues:
1) I would like the plugin to search PDF files attached to posts in a certain category (Journals). I would like the search results to display ONLY PDFs that are attached to posts in that category (no unattached PDFs, no PDFs attached to posts in other categories or to pages).
I found this post, which is similar, but it uses a custom post type rather than a category:
https://wp-dreams.com/forums/topic/media-search-results/2) I would also like for the search results to display an excerpt of the PDF text, but link to the parent post. In my current configuration, if I search for the word “test”, I do get the correct behavior in the search dropdown.
However, if I click through to the search page by hitting enter, my search results link to the attachment directly and not to the parent page. Also, I would like this page to show a excerpt of text from the PDF (I think my theme is causing it not to show, because I have excerpts disabled for the blog archive).
3) And finally, have you done the release discussed in this post, for creating images from PDFs?
https://wp-dreams.com/forums/topic/cant-get-good-pretty-pdf-search-results-like-your-demo/Thank you very much for your help, and happy new year.
January 3, 2023 at 1:47 pm #40693Ernest Marcinko
KeymasterHi,
1. In theory, something like this could do the trick:
add_filter( 'asp_results', 'asp_attachment_parent_in_category', 10, 1 ); function asp_attachment_parent_in_category( $results ) { // Category IDs $categories = array(1, 2, 3); 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) ) { if ( !in_category($categories, $parent_id) ) { unset($results[$k]); } } } } return $results; }This only works with categories – no other taxonomy terms or anything else. The $categories variable holds the category IDs for which you want the parents to be restricted, don’t forget to change that.
2. This may not be possible without actual changes on the theme. The plugin forces the same titles/contents/links for the results page, but usually the results page queries these fields individually and therefore it refreshes them from the database.
There is still hope though, try this custom code:
add_filter( 'asp_regular_search_result', 'asp_force_guids', 10, 1 ); function asp_force_guids( $r ) { if ( isset($r->asp_data) ) { $r->guid = $r->asp_data->link; $r->post_excerpt = $r->asp_data->content; $r->post_content = $r->asp_data->content; } return $r; }3. Not yet, there are potential security vulerability concerns that has to be resolved to make it safe for release. It will be implemented for sure though.
January 4, 2023 at 3:31 am #40731ballerinamoose
ParticipantHi,
Thanks so much for the reply.
1. Unfortunately did not work. To test it, I have three PDFs–only one is attached to a post in the desired category, one is attached to a post in another category, and the last is unattached. All three still show up in search.
2. This partially worked. The results page now shows the snippet, but it still links directly to the PDF and not to the parent post.
3. Good to hear.
Thank you!
January 4, 2023 at 11:21 am #40737Ernest Marcinko
KeymasterHi,
1. Try this variation:
add_filter( 'asp_results', 'asp_attachment_parent_in_category', 10, 1 ); function asp_attachment_parent_in_category( $results ) { // Category IDs $categories = array(1, 2, 3); 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) ) { if ( $parent_id == 0 || !in_category($categories, $parent_id) ) { unset($results[$k]); } } else { unset($results[$k]); } } } return $results; }2. That is bad news, this may not be possible without theme modifications then. Maybe a bit more aggressive code will do it, try this:
add_filter( 'asp_regular_search_result', 'asp_force_guids', 10, 1 ); function asp_force_guids( $r ) { if ( isset($r->asp_data) ) { if ( $r->post_type == 'attachment' ) { $parent_id = wp_get_post_parent_id( $r->id ); if ( !is_wp_error($parent_id) ) { $r->guid = get_permalink($parent_id); } } else { $r->guid = $r->asp_data->link; } $r->post_excerpt = $r->asp_data->content; $r->post_content = $r->asp_data->content; } return $r; }January 5, 2023 at 12:26 am #40763ballerinamoose
ParticipantUnfortunately, neither of these worked.
1. This is still showing PDFs from other categories, unattached PDFs, and PDFs attached to pages. I edited the page listed in the private details so it would be easy for you to test & replicate.
2. This did not change the results, but I am less concerned about this problem. I can always disable the search page and force the search dropdown instead. Unless you have another idea.
Thank you for your help!
January 5, 2023 at 1:03 pm #40768Ernest Marcinko
KeymasterHi,
1.Okay, I have logged in to check and made a tiny modification. Can you please test if this changed anything.
For reference the new code is:
add_filter( 'asp_results', 'asp_attachment_parent_in_category', 10, 1 ); function asp_attachment_parent_in_category( $results ) { // Category IDs $categories = array(28); foreach ($results as $k=>&$r) { if ( isset($r->post_type) && $r->post_type == 'attachment' ) { $parent_id = wp_get_post_parent_id( $r->id ); if ( !is_wp_error($parent_id) && !empty($parent_id) ) { if ( $parent_id == 0 || !in_category($categories, $parent_id) ) { unset($results[$k]); } } else { unset($results[$k]); } } } return $results; }January 5, 2023 at 5:33 pm #40797ballerinamoose
ParticipantThat worked!!! This feature will make a lot of people very happy. 🙂 Thank you so much!
One more question–and this may not matter because I’m intending to disable the search results page–but now at the top of the search results page it is outputting things like this:
string(8) “pagepost” int(0) string(8) “pagepost” int(0) string(8) “pagepost” int(10570) string(8) “pagepost” int(10348)
string(8) “pagepost” int(10348)Is that a problem?
Thank you!
January 5, 2023 at 5:38 pm #40798Ernest Marcinko
KeymasterGreat 🙂 At least it works now.
I removed that extra text – I used that for debugging the code, and I forgot to delete that line. I have quickly logged in and corrected that. It should be fine now.
January 5, 2023 at 6:51 pm #40799ballerinamoose
ParticipantExcellent. Thank you so much! I really appreciate all the help! 🙂
January 5, 2023 at 7:13 pm #40800Ernest Marcinko
KeymasterYou cannot access this content.
January 5, 2023 at 8:34 pm #40801ballerinamoose
ParticipantYou cannot access this content.
-
AuthorPosts
- The topic ‘PDF search results’ is closed to new replies.