WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to add search filter by custom values? (6 posts)

  1. takien
    Member
    Posted 4 years ago #

    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?

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    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.

  3. takien
    Member
    Posted 4 years ago #

    wow...

    many many thanks t31os_
    it's work perfect...

    :)

  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    No problem, i'll mark this resolved... ;)

  5. sballinger26
    Member
    Posted 3 years ago #

    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

  6. Mr. Alexander
    Member
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic