How to exclude certain media files from the search?

Home Forums Product Support Forums Ajax Search Pro for WordPress Support How to exclude certain media files from the search?

This topic contains 9 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 8 months, 1 week ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #43065
    Studio Hirilo
    Studio Hirilo
    Participant

    Hi,
    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 !

    #43072
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    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.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #43075
    Studio Hirilo
    Studio Hirilo
    Participant

    Hello !
    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.

    #43076
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    You 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 ) {

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #44380
    Studio Hirilo
    Studio Hirilo
    Participant

    Hello,
    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 ! 🙂

    #44382
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Sure!

    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.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #44383
    Studio Hirilo
    Studio Hirilo
    Participant

    Darn… it still doesn’t work… 🙁

    #44408
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    I 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;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #44410
    Studio Hirilo
    Studio Hirilo
    Participant

    Hello!
    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!
    Bye

    #44411
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Neat 🙂 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.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.