Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Ignore unlinked Attachments
This topic contains 11 replies, has 2 voices, and was last updated by AdditiveFX 4 years, 8 months ago.
- AuthorPosts
- March 5, 2019 at 2:21 pm #21401
I wish to search for attachments that are attached to some page or post. I have implemented this in PHP by filtering the results but I can not do that in the AJAX results.
Ideally this would be an option in the highlighted part of my screenshot (“Link results to – attachment parent post – and if parent does not exist then – IGNORE RESULT”) or something like this.
Please get in touch with me abou this.
Attachments:
You must be logged in to view attached files.March 5, 2019 at 4:35 pm #21403Hi!
While there is no feature/option for that on the back-end, it should be possible by using a custom code. Try adding this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!
add_filter('asp_results', 'asp_results_remove_unlinked_attachments'); function asp_results_remove_unlinked_attachments($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) ) { // Remove the post from the results list unset($results[$k]); } } } return $results; }
I hope this helps!
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
March 5, 2019 at 4:47 pm #21404Hey, thanks for the speedy reply 🙂
I have implemented this and it doesn’t seem to work :/ Is this supposed to also filter the AJAX results?
Right now there is no difference in the results, do I need to empty some sort of cache in your plugin for the changes to take effect?Thanks!
March 5, 2019 at 4:50 pm #21405Hi,
Yep, that should filter the live results. I have made a correction to the code right after I posted, you might still got the original post. Can you please verify, that this is the code:
add_filter('asp_results', 'asp_results_remove_unlinked_attachments'); function asp_results_remove_unlinked_attachments($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) ) { // Remove the post from the results list unset($results[$k]); } } } return $results; }
You don’t have to clear the cache, this should work effective immediately.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
March 8, 2019 at 3:25 pm #21512Hi,
I have tried this and no luck.I think I know why it is not working: when I return $r->content_type somewhere visible it’s always “pagepost” – never “attachment”
Is there something wrong in my settings?
add_filter('asp_results', 'asp_results_remove_unlinked_attachments'); function asp_results_remove_unlinked_attachments($results) { foreach ($results as $k=>&$r) { $results[$k]->title = $r->content_type; } return $results; }
this always returns “pagepost” as the search item title.
March 8, 2019 at 3:27 pm #21513Hi,
I think you might have an older version of the plugin, I am not sure. In that case, this modification should do the trick:
Best,add_filter('asp_results', 'asp_results_remove_unlinked_attachments'); function asp_results_remove_unlinked_attachments($results) { 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) ) { // Remove the post from the results list unset($results[$k]); } } } return $results; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
March 8, 2019 at 3:32 pm #21514I have version 4.14.6, on codecanyon that’s the newest version, right?
March 8, 2019 at 3:37 pm #21515Hi,
Yes, that is the latest one. I checked the source on the attachment search class, and the attachment results shoul have the $r->content_type as ‘attachment’. If it’s ‘pagepost’, then it’s a different post type in that case.
Try testing it by printing the post type to the title:
add_filter('asp_results', 'asp_results_remove_unlinked_attachments'); function asp_results_remove_unlinked_attachments($results) { foreach ($results as $k=>&$r) { $results[$k]->title = $r->post_type; } return $results; }
It shouldn’t be ‘attachment’, but something else (?).
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
March 8, 2019 at 3:42 pm #21517Hey,
it works now with your proposed fix! The results are indeed of post-type “attachment”. Content type is still “pagepost” for everything. When I print the post-type somewhere I get sensible strings like “attachment”, “event”, “page” and “post”
This is solved. Would be interesting to know why the first solution doesn’t work for me.
I’m using the index table engine, I have several custom post types in WordPress, I have indexed all of them including file contents – Seems like a normal use of your plugin, doesn’t it? 🙂
Btw. this is an awesome plugin! Really happy with it!March 8, 2019 at 3:43 pm #21518To be clear, this is the working code:
add_filter('asp_results', 'asp_results_remove_unlinked_attachments'); function asp_results_remove_unlinked_attachments($results) { 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) ) { // Remove the post from the results list unset($results[$k]); } } } return $results; }
March 8, 2019 at 4:35 pm #21519That’s it, the index table engine, I was looking at the wrong file there 🙂
Best,
There is a bug in there, the content_type is not set correctly, I will make sure to fix that in the upcoming release. Glad to know the second one works 🙂
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
March 8, 2019 at 4:38 pm #21520perfect! Thanks for your help
- AuthorPosts
You must be logged in to reply to this topic.