Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Search for media and exclude search from media in custom post type
This topic contains 9 replies, has 2 voices, and was last updated by Ernest Marcinko 1 year, 2 months ago.
- AuthorPosts
- January 19, 2022 at 8:30 am #36307
Hello. I need a search bar that will search only in attachments. This is possible by enabling the attachment search in the settings, no problem there. However, how can I exclude the search for attachments from a specific custom post type? By allowing for example only posts in the “Search in Custom post types” option, I still get attachments that have been uploaded for a custom post type (bildergallerie). I have uploaded a test.pdf through a custom field inside a post of this custom post type and although I enabled only posts, I still get a match if I search for “test” (which is this pdf).
It would be great if I could exclude documents/attachments from this custom post type.And another question. I have another search instance that searches for posts from another custom post type. The number of posts from this custom post type is over 5000 but, as I enabled the load more feature that displays the next 50 posts per click, the total number I get is less than 500. I clicked through all the load more steps and indeed I didn’t get more than that. How can I correct this error?
Many thanks in advance.
January 19, 2022 at 2:51 pm #36314Hi,
Well, the first issue might be possible to resolve via a custom code, but I am not entirely sure though. If the post parent ID is not set via the custom field input, then it is not going to work.
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_remove_attachments_where_post_type', 10, 1); function asp_custom_footer_script( $results ) { $excluded = array( 'post_type_name1', 'post_type_name2', ); foreach ( $results as $k => $r ) { if ( isset($r->post_type) && $r->post_type == 'attachment' ) { $parent_id = wp_get_post_parent_id($r->id); if ( $parent_id != 0 ) { if ( in_array(get_post_type($parent_id), $excluded) ) { unset($results[$k]); } } } } return $results; }
You need to change the $excluded variable for the post type names you want to exclude the attachment results for.
The second issue is kind of intentional, it is to limit the number of iterations to 10x on the more results feature. It can be changed via a custom code as well:
Best,add_filter( 'asp_query_args', 'asp_change_remaining_limit_mod', 10, 2 ); function asp_change_remaining_limit_mod( $args, $id ) { $args['_remaining_limit_mod'] = 9999; return $args; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
January 19, 2022 at 4:07 pm #36315Thanks Ernest. Regarding the 2nd issue, the code worked perfectly, many thanks.
Regarding the 1st issue I still get some problems. First I corrected the
function asp_custom_footer_script( $results ) {
part to
function asp_remove_attachments_where_post_type( $results ) {
as the filter was named like that :).
but again I get results from pages or normal posts… Ideally I need dokuments that I have uploaded through a file upload field I created through advanced custom fields pro for this specific custom post type (‘newsroom’) :/PS. Why “if ( isset($r->post_type) && $r->post_type == ‘attachment’ ) {” …? I have created this custom post type like this:
register_post_type( 'newsroom', array( 'label' => __( 'newsroom' ), 'labels' => array( 'name' => __( 'Newsroom' ), 'singular_name' => __( 'Newsroom' ), 'add_new' => 'Neuen Newsroom Beitrag einfügen', ), 'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', ), 'hierarchical' => false, 'rewrite' => array('slug' => 'newsroom','with_front' => false), 'public' => true, 'show_ui' => true, 'menu_icon' => 'dashicons-media-spreadsheet', 'show_in_menu' => true, 'show_in_nav_menus' => true, 'show_in_admin_bar' => true, 'menu_position' => 5, 'can_export' => true, 'has_archive' => false, 'exclude_from_search' => true, 'publicly_queryable' => false, 'capability_type' => 'post' ) );
-
This reply was modified 1 year, 2 months ago by
zisis19.
January 19, 2022 at 4:11 pm #36316Hi,
Try enabling searching only in attachments and nothing else (no post types, nothing), let’s see if anything is returned then.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
January 19, 2022 at 4:15 pm #36318The same :/ . Funny enough, when I exclude in the exclude array my custom post type, I don’t get results from them, but when I exclude ‘post’, ‘page’, the search function finds everything, even the ones of the custom post type
January 19, 2022 at 4:57 pm #36319add_filter('asp_results', 'asp_remove_attachments_where_post_type', 10, 1); function asp_remove_attachments_where_post_type( $results ) { $included = array( 'newsroom' ); foreach ( $results as $k => $r ) { if ( isset($r->post_type) && $r->post_type == 'attachment' ) { $parent_id = wp_get_post_parent_id($r->id); if ( $parent_id != 0 ) { if ( in_array(get_post_type($parent_id), $included) ) { return $results; } } } } unset($results); }
This did the trick. Instead of exluding an array I included it… Don’t know why the other way round didn’t work… I am just happy that it works now. Thank you for the initial code 🙂 .
PS: In the Plugin settings I removed any restrictions I had tried out regarding CPTs or Custom Fields.
PS2: One last question. Is it possible to limit this code only for one specific search instance? I named this one Dokumentenfinder as it searches only for dokuments. But what if I need to create a new search bar that will search for pages, posts etc… ? I created a second search instance for this as a test and I see that the above code applies to this new search instance as well. When I remove it, the 2nd one works just as it should… So, how can we limit this code only to the “Dokumentenfinder” search instance? It would be a shame not to be able to create more search instances in the future if needed :/.
January 20, 2022 at 6:18 am #36320Goodmorning Ernest. I am sorry. Yesterday I was so tired that I completely forgot what I wanted to achieve in the first place with this search field. Your original code is working perfectly. In the excluded array, when I put my custom post type it indeed excludes attachments from it. That was my goal. So thank you for your help.
If you could answer also the question, how I can limit this code only to this search instance and not let it affect future ones, that would be great. Thanks 🙂January 20, 2022 at 10:21 am #36323No problem, I too have long workdays, at the end I usually make a lot of mistakes.
You can of course limit the code to a specific search ID:
add_filter('asp_results', 'asp_remove_attachments_where_post_type', 10, 2); function asp_remove_attachments_where_post_type( $results, $search_id ) { if ( $search_id == 1 ) { $excluded = array( 'post_type_name1', 'post_type_name2', ); foreach ( $results as $k => $r ) { if ( isset($r->post_type) && $r->post_type == 'attachment' ) { $parent_id = wp_get_post_parent_id($r->id); if ( $parent_id != 0 ) { if ( in_array(get_post_type($parent_id), $excluded) ) { unset($results[$k]); } } } } } return $results; }
I hope this works 🙂
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
January 20, 2022 at 10:51 am #36330Thanks Ernest. You are a hero!!
January 20, 2022 at 10:52 am #36331You cannot access this content. Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
-
This reply was modified 1 year, 2 months ago by
- AuthorPosts
You must be logged in to reply to this topic.