limit search

This topic contains 12 replies, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 3 months, 3 weeks ago.

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #39533
    23volt
    23volt
    Participant

    Hello friend!

    First of all: thank you so much for your great plugin. You save so much time and its a great work!
    Right now we have this situation:

    Each post have different parents. We know we can restrict your search by parent.
    But now we also need to limit the search by customfield value.

    Example:
    If user start search in search widget TEST:
    –> Show only results with parent abc OR customfield=abc

    So finaly the result will only show results which parent abc or customfield value customfield=abc

    I attached the code which we are using now. But how to extend it to limited search also to customfield?
    We can pay you, if you say this is not very easy and we will not waste your time. 🙂

    BR
    Tommy

    add_filter( ‘asp_query_args’, ‘asp_include_only_parent_ids’, 10, 2 );
    function asp_include_only_parent_ids( $args, $id ) {

    if ( $id == 3 ) {
    $args[‘post_parent’] = array(11748);

    } else if ( $id == 12 ) {
    $args[‘post_parent’] = array(3239);
    }

    return $args;
    }

    #39540
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi,

    Thank you very much for your kind words!

    It should not be too difficult, something like this:

    add_filter( 'asp_query_args', 'asp_include_only_parent_ids', 10, 2 );
    function asp_include_only_parent_ids( $args, $id ) {
    
    	if ( $id == 3 ) {
    		$args['post_parent'] = array(11748);
    	} else if ( $id == 12 ) {
    		$args['post_parent'] = array(3239);
    	}
    	
    	$args['post_meta_filter'][] = array(
    		'key'     => 'customfield', // meta key
    		'value'   => 'abc'
    		'operator' => 'LIKE',	   // You can also try ELIKE here for more strict match
    		'allow_missing' => false   // allow match if this custom field is unset
    	);
    
    	return $args;
    }

    You may need to try the “ELIKE” operator as well to have an exact field match, as well as try the “allow_missing” argument on true if you want results where this field does not exist.

    I hope this helps!

    Best,
    Ernest Marcinko

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


    #39542
    23volt
    23volt
    Participant

    Wow, thats great. Please send me your paypal for a “thank you”. 🙂

    One question i have.
    How to have this please:

    if ( $id == 3 ) {
    $args[‘post_parent’] = array(11748) OR customfield = abc;
    } else if ( $id == 12 ) {
    $args[‘post_parent’] = array(3239) OR customfield = xyz;

    So search widget with id 3 need to fit post parent id 11748 OR customfield abc.
    And widget id 12 need to fit post parent id 3239 OR customfield = xyz.
    Some posts are a customfield and some not.

    Do you can help us to solve that issue?

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


    #39544
    23volt
    23volt
    Participant

    Hello Ernest,

    but in your code here

    add_filter( 'asp_query_args', 'asp_include_only_parent_ids', 10, 2 );
    function asp_include_only_parent_ids( $args, $id ) {
    
    	if ( $id == 3 ) {
    		$args['post_parent'] = array(11748);
    	} else if ( $id == 12 ) {
    		$args['post_parent'] = array(3239);
    	}
    	
    	$args['post_meta_filter'][] = array(
    		'key'     => 'customfield', // meta key
    		'value'   => 'abc'
    		'operator' => 'LIKE',	   // You can also try ELIKE here for more strict match
    		'allow_missing' => false   // allow match if this custom field is unset
    	);
    
    	return $args;
    }

    there is not this logic integrated:
    If widget ID 3 –> post_parent = 11748 OR customfield = abc;
    If widget ID 12 –> post_parent = 5000 OR customfield = xyz;

    There is a chance to archive this?

    #39545
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Sure, it’s like this:

    add_filter( 'asp_query_args', 'asp_include_only_parent_ids', 10, 2 );
    function asp_include_only_parent_ids( $args, $id ) {
    
    	if ( $id == 3 ) {
    		$args['post_parent'] = array(11748);
    		$args['post_meta_filter'][] = array(
    			'key'     => 'customfield', 
    			'value'   => 'abc'
    			'operator' => 'LIKE',	 
    			'allow_missing' => false  
    		);
    	} else if ( $id == 12 ) {
    		$args['post_parent'] = array(3239);
    		$args['post_meta_filter'][] = array(
    			'key'     => 'customfield', 
    			'value'   => 'xyz'
    			'operator' => 'LIKE',	   
    			'allow_missing' => false   
    		);
    	}
    
    	return $args;
    }
    Best,
    Ernest Marcinko

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


    #39546
    23volt
    23volt
    Participant
    You cannot access this content.
    #39551
    23volt
    23volt
    Participant

    • This reply was modified 3 months, 4 weeks ago by 23volt .
    • This reply was modified 3 months, 4 weeks ago by 23volt .
    #39561
    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 :)


    #39609
    23volt
    23volt
    Participant
    You cannot access this content.
    #39622
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Thank you for your kind words 🙂

    1. I am not a 100% sure, but from the output it seems like an incorrect post type was selected (something like “revision” or a similar name). Make sure that only the required post types are selected.

    Another possible issue might be, that the post statuses are entered incorrectly. On the same options page (Search Sources -> Post type search), make sure that only the publish post status is entered.

    2. Yes, it is possible, by passing the values as arrays, see this code:

    add_filter('asp_query_cpt', 'asp_change_query_directly', 10, 1);
    function asp_change_query_directly($q) {
    	global $wpdb;
    
    	return str_replace("AND $wpdb->posts.post_parent IN", "OR $wpdb->posts.post_parent IN", $q);
    }
    
    add_filter( 'asp_query_args', 'asp_include_only_parent_ids', 10, 2 );
    function asp_include_only_parent_ids( $args, $id ) {
    
    	if ( $id == 3 ) {
    		$args['post_parent'] = array(11748);
    			$args['post_meta_filter'][] = array(
    				'key'     => 'ad_location', 
    				'value'   => array('62b9acbc2709e', '123', 'ABC'),
    				'operator' => 'ELIKE',
    				'allow_missing' => false  
    			);
    		}
    
    	return $args;
    }
    Best,
    Ernest Marcinko

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


    #39626
    23volt
    23volt
    Participant

    1. I am not a 100% sure, but from the output it seems like an incorrect post type was selected (something like “revision” or a similar name). Make sure that only the required post types are selected.

    We have this results with all post types. I created new post type and select only this one in your plugin.
    Then starting a serch. This ends also into this: Slides from elementor widgets are shown in search.

    Another possible issue might be, that the post statuses are entered incorrectly. On the same options page (Search Sources -> Post type search), make sure that only the publish post status is entered.

    I checked this settings. We setup “publish” and we still see the elementor widget content in search result.

    Last but not least:
    When i remove your custom code the search does not show any content from elementor widget.
    When i add your custom code into functions.php -> i see elements from slider widget again in the search.
    So the issue depends to your code. You can help us if we pay for your work per hour?

    BR
    Tommy

    • This reply was modified 3 months, 3 weeks ago by 23volt .
    #39636
    Ernest Marcinko
    Ernest Marcinko
    Keymaster

    Hi Tommy,

    Oh, then it was the custom code causing the issue as I predicted earlier. Changing the logic to an OR basically negated every other criteria for the search query. One last thing to try is to inject the post type and post status queries to the first code:

    add_filter('asp_query_cpt', 'asp_change_query_directly', 10, 1);
    function asp_change_query_directly($q) {
    	global $wpdb;
    	$q = preg_replace('/AND ' . preg_quote("$wpdb->posts.post_parent IN ") . '\((.*?)\)/im',
    		"OR ($wpdb->posts.post_type IN ('post', 'page', 'dealer') AND $wpdb->posts.post_status LIKE 'publish' AND $wpdb->posts.post_parent IN($1))",
    		$q);
    	return $q;
    }
    
    add_filter( 'asp_query_args', 'asp_include_only_parent_ids', 10, 2 );
    function asp_include_only_parent_ids( $args, $id ) {
    
    	if ( $id == 3 ) {
    		$args['post_parent'] = array(11748);
    			$args['post_meta_filter'][] = array(
    				'key'     => 'ad_location', 
    				'value'   => array('62b9acbc2709e', '123', 'ABC'),
    				'operator' => 'ELIKE',
    				'allow_missing' => false  
    			);
    		}
    
    	return $args;
    }

    Unfortunately I am too busy to do do custom jobs, I am sorry.

    Best,
    Ernest Marcinko

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


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

You must be logged in to reply to this topic.