WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] How to add search filter by custom values?

[Resolved] How to add search filter by custom values?

  • I’m trying to this..

    function my_search_filter($where) {
    $where .= " AND wp_postmeta.meta_key = city AND wp_postmeta.meta_value ='Jakarta'";
    return $where;
    }
    
    if(is_search()) {
    add_filter('posts_where','my_search_filter');
    }

    but it does not work…

    any idea?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Perhaps take a slightly different approach..

    add_filter( 'pre_get_posts' , 'add_meta_to_search' );
    function add_meta_to_search( $query ) {
    	if( $query->is_search ) {
    		$query->set( 'meta_key' , 'city' );
    		$query->set( 'meta_value' , 'Jakarta' );
    	}
    	return $query;
    }

    When you’re checking is_search() you’re actually at the point where the query is setup and ready to run, so the posts_where filter is too late in the process of things, so to speak.

    wow…

    many many thanks t31os_
    it’s work perfect…

    🙂

    No problem, i’ll mark this resolved… 😉

    hi
    i have three tabs(sections) in search page. i want to filter my search as per tabs posts for eg:
    1st tab is Post
    2nd tab is upcoming events
    3nd tab is past events

    any help would be appriciated

    Thank in advance

    Hello,

    I am trying to do a similar task but using the LIKE term instead of an exact match on the meta_value.

    How should I go about that ?

    Thank you for your help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] How to add search filter by custom values?’ is closed to new replies.