Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › How to exclude certain media files from the search?
- This topic has 9 replies, 2 voices, and was last updated 2 years, 11 months ago by
Ernest Marcinko.
-
AuthorPosts
-
June 14, 2023 at 10:32 am #43065
Studio Hirilo
ParticipantHi,
I’m trying to create a search bar that only returns audio and PDF files. I would like to include only a few selected media files. However, I’m unable to achieve what I want. I’ve seen that Ajax Search Pro takes into account custom fields from ACF, but it’s not working for me. How can I proceed? I can’t find a ready-made solution.
Maybe this could work by using keywords contained in the file names? Thank you for helping me find a solution.
Have a good day/evening !June 15, 2023 at 9:38 am #43072Ernest Marcinko
KeymasterHi,
Thank you very much for your kind words and for the details it helps me a lot!
I believe the best way to get around this is by using this option to exlude media files by their IDs.
Unfortuntely there is no option to include only specific media files, but it should be still doable via using a small custom code snippet:
add_filter("asp_query_args", "asp_query_args_change", 10, 2); function asp_query_args_change($args, $search_id) { $include_ids = array(1, 2, 3); // Media file IDS to include // No change below $ids = implode(', ', $include_ids); $args['attachment_query']['where'] = "AND $wpdb->posts.ID IN($ids)"; return $args; }In this code you only need to change the $include_ids variable and add the meida file IDs which you want to see in the results list. Try adding this code via the Code Snippets plugin or 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.
June 15, 2023 at 10:18 am #43075Studio Hirilo
ParticipantHello !
Thank you very much for your prompt response. I will try that with Code Snippet. I just have one thing I forgot to mention, oops: I will create multiple search instances, and each one should include different files IDs. How can I associate each specific code with the different instances? Thank you for your answers!
See you soon.June 15, 2023 at 10:22 am #43076Ernest Marcinko
KeymasterYou are welcome π
No problem, that is also possible via this changed code:
add_filter("asp_query_args", "asp_query_args_change", 10, 2); function asp_query_args_change($args, $search_id) { if ( $search_id == 1 ) { $include_ids = array(1, 2, 3); // Media file IDS to include // No change below $ids = implode(', ', $include_ids); $args['attachment_query']['where'] = "AND $wpdb->posts.ID IN($ids)"; return $args; } }Just change the number on line with the ID of the search for which this code should be effective:
if ( $search_id == 1 ) {June 20, 2023 at 1:00 pm #44380Studio Hirilo
ParticipantHello,
Thank you for this modification! I finally had time to test this code in Code Snippet. Unfortunately, it doesn’t seem to work for meβ¦ I’m sorry, I’m really not proficient in PHP and I usually manage to figure things out on my ownβ¦ but not this time! πCould you please take another look? Here is the modified code:
add_filter("asp_query_args", "asp_query_args_change", 10, 2); function asp_query_args_change($args, $search_id) { if ( $search_id == 1 ) { $include_ids = array(1954, 1955, 1956, 1957, 1958); // Media file IDS to include // No change below $ids = implode(', ', $include_ids); $args['attachment_query']['where'] = "AND $wpdb->posts.ID IN($ids)"; return $args; } }I didn’t change the ID of $search_id because I’m testing it with the first search instance I created, and I only modified the list of media IDs. I don’t see what could be wrong…
I cleared all caches to make sure, but nothing seems to work.
I created the code in Code Snippet as PHP code, allowing it to be active everywhere on the site…Thank you for assisting me!
Have a nice day ! πJune 20, 2023 at 1:09 pm #44382Ernest Marcinko
KeymasterSure!
Can you try this variation maybe:
add_filter("asp_query_args", "asp_query_args_change", 10, 2); function asp_query_args_change($args, $search_id) { global $wpdb; if ( $search_id == 1 ) { $include_ids = array(1954, 1955, 1956, 1957, 1958); // Media file IDS to include // No change below $ids = implode(', ', $include_ids); $args['attachment_query']['where'] = "AND $wpdb->posts.ID IN($ids)"; } return $args; }This one has the global database variable request so if that was the issue, then this may change something.
June 20, 2023 at 1:22 pm #44383Studio Hirilo
ParticipantDarn… it still doesn’t work… π
June 21, 2023 at 10:04 am #44408Ernest Marcinko
KeymasterI finally had a chance to test, this is the one:
add_filter("asp_query_args", "asp_query_args_change", 10, 2); function asp_query_args_change($args, $search_id) { global $wpdb; if ( $search_id == 1 ) { $include_ids = array(1954, 1955, 1956, 1957, 1958); // Media file IDS to include // No change below $ids = implode(', ', $include_ids); $args['attachment_query']['where'] = "AND $wpdb->posts.ID IN($ids)"; $args['post_in'] = $include_ids; } return $args; }June 21, 2023 at 10:07 am #44410Studio Hirilo
ParticipantHello!
I found the issue!!! π
It turns out my search instance was using the index table engine… All I had to do was switch to the regular engine, and… miracle, the code worked!!!Thank you once again for your valuable assistance.
I wish you all the best!
ByeJune 21, 2023 at 10:09 am #44411Ernest Marcinko
KeymasterNeat π You are welcome!
The code in my previous reply should work with both, just in case you need it.
If you don’t mind, I will close this topic soon and mark it as resolved, feel free to open another one if you have other questions or issues.
If you like the plugin and have not rated already, feel free to leave a rating on your codecanyon downloads page and on the wordpress plugin repository, it’s greatly appreciated.
-
AuthorPosts
- You must be logged in to reply to this topic.