Search for media and exclude search from media in custom post type

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 Ernest Marcinko 8 months, 2 weeks ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #36307
    zisis19
    zisis19
    Participant

    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.

    #36314
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    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:

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

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


    #36315
    zisis19
    zisis19
    Participant

    Thanks 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 8 months, 2 weeks ago by zisis19 zisis19.
    #36316
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    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 :)


    #36318
    zisis19
    zisis19
    Participant

    The 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

    #36319
    zisis19
    zisis19
    Participant
    add_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 :/.

    #36320
    zisis19
    zisis19
    Participant

    Goodmorning 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 🙂

    #36323
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    No 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 :)


    #36330
    zisis19
    zisis19
    Participant

    Thanks Ernest. You are a hero!!

    #36331
    Ernest Marcinko
    Ernest Marcinko
    Keymaster
    You cannot access this content. 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.