Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Show document related posts
This topic contains 19 replies, has 2 voices, and was last updated by Peter Sippel 1 year, 4 months ago.
- AuthorPosts
- October 14, 2022 at 11:33 am #39653
Hello.
I want to achieve the following:1. User adds a document to the post (will result in a link to the doc). The document contains the word “MySearchTest”
2. AjaxSearchPro finds the document, knows the file url/name of the document
3. AjaxSearchPro finds the post where the document was added (as a link)
4. This post will be in the results, just as some other posts who have “MySearchText” in the post contentFrom a former inquiry I had this reply:
“However it is possible via some minor custom code snippet to replace the document in the results with the post. In case you purchase, you can open a support ticket with this and we will suggest a possible modification.”Please advise accordingly.
Thanks. PeterOctober 14, 2022 at 12:23 pm #39654Hi Peter,
Thank you for contacting us!
First, you need to enabled and test the search for media file contents. Here is the full documentation on how you can do that.
Once you get that going, and the media results are appearing in the live results list, then apply the custom code below.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_return_posts_by_attachment_results'); function asp_return_posts_by_attachment_results($results) { $ids = array(); $parent_ids = array(); $new_results = array(); // Gather existing post results first foreach($results as $k=>&$r){ if ( isset($r->post_type) ) { if ( $r->post_type == 'attachment' ) { $parent_ids[] = wp_get_post_parent_id($r->id); unset($results[$k]); } else { $ids[] = $r->id; } } } $parent_ids = array_unique(array_diff($parent_ids, $ids, array(0))); foreach ( $parent_ids as $id ) { $res = new stdClass(); $res->id = $id; $res->title = get_the_title($id); $res->content = get_the_content(null, false, $id); $res->image = \WPDRMS\ASP\Utils\Post::parseImage($res, array( 'get_content' => false, 'image_sources' => array('featured', 'content'), 'apply_the_content' => true) ); $res->content = wd_substr_at_word(strip_tags($res->content), 130); $res->post_type = get_post_type($id); $res->link = get_permalink( $id ); $res->content_type = 'pagepost'; $new_results[] = $res; } return array_merge($new_results, $results); }
This custom code removes the attachments, and replaces them with the posts where they were originally attached to.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
October 14, 2022 at 10:15 pm #39660Hi Ernest,
thanks for the code, but it’s not working.
1. When I click on the item in the found box, I’m routed directly to the file post
2. When I click on the magnifyer to see the results page, I get a critical errorPlease advise.
Thanks. PeterOctober 17, 2022 at 12:10 pm #39668Hi,
I may have to take a look at this directly, it is working on our test servers unfortunately.
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 :)
October 17, 2022 at 4:32 pm #39675You cannot access this content.October 18, 2022 at 10:36 am #39684You cannot access this content.October 18, 2022 at 12:31 pm #39690You cannot access this content. Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
October 18, 2022 at 12:56 pm #39691You cannot access this content.October 19, 2022 at 1:43 pm #39713Thank you!
I made a slight correction to the code, now it seems to be working. I tried to search for a few documents, like “API-desc-guidelines2411” and it seems to be returning only the post now.
For reference, the updated code is:
Best,add_filter('asp_results', 'asp_return_posts_by_attachment_results'); function asp_return_posts_by_attachment_results($results) { $ids = array(); $parent_ids = array(); $new_results = array(); // Gather existing post results first foreach($results as $k=>&$r){ if ( isset($r->post_type) ) { if ( $r->post_type == 'attachment' ) { $parent_ids[] = wp_get_post_parent_id($r->id); unset($results[$k]); } else { $ids[] = $r->id; } } } $parent_ids = array_unique(array_diff($parent_ids, $ids, array(0))); foreach ( $parent_ids as $id ) { $res = new stdClass(); $res->id = $id; $res->title = get_the_title($id); $res->content = get_the_content(null, false, $id); $res->image = get_the_post_thumbnail_url($id); $res->content = wd_substr_at_word(strip_tags($res->content), 130); $res->post_type = get_post_type($id); $res->link = get_permalink( $id ); $res->content_type = 'pagepost'; $new_results[] = $res; } return array_merge($new_results, $results); }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
October 19, 2022 at 4:13 pm #39715You cannot access this content.October 19, 2022 at 4:57 pm #39719Thanks!
Best,
The snippet was added incorrectly, it was only allowed to execute on the site front-end, but the search requests are made on the back-end. Changing the snippet to execute everywhere seems to have resolved the issue.
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
October 19, 2022 at 5:20 pm #39720You cannot access this content.October 20, 2022 at 9:01 am #39726Yes, I think so – it has to be added via the code, as those elements are added programmatically, and some of the options will not apply to them. Please change the custom code to this:
Best,add_filter('asp_results', 'asp_return_posts_by_attachment_results'); function asp_return_posts_by_attachment_results($results) { $ids = array(); $parent_ids = array(); $new_results = array(); // Gather existing post results first foreach($results as $k=>&$r){ if ( isset($r->post_type) ) { if ( $r->post_type == 'attachment' ) { $parent_ids[] = wp_get_post_parent_id($r->id); unset($results[$k]); } else { $ids[] = $r->id; } } } $parent_ids = array_unique(array_diff($parent_ids, $ids, array(0))); foreach ( $parent_ids as $id ) { $res = new stdClass(); $res->id = $id; $res->title = get_the_title($id); $res->content = get_the_content(null, false, $id); $res->image = get_the_post_thumbnail_url($id); $res->content = wd_substr_at_word(strip_tags($res->content), 130); $res->post_type = get_post_type($id); $res->link = get_permalink( $id ); $res->content_type = 'pagepost'; $res->date = @date_i18n(get_option('date_format', "Y-m-d") . " " . get_option('time_format', "H:i:s"), get_post_time('U', false, $id)); $res->author = get_the_author_meta( 'display_name' , get_post_field ('post_author', $id)); $new_results[] = $res; } return array_merge($new_results, $results); }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
October 20, 2022 at 10:46 am #39734Perfect ! Thank you so much !
October 20, 2022 at 12:11 pm #39737You cannot access this content. Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
- AuthorPosts
You must be logged in to reply to this topic.