WordPress Search with multiple Custom Fields

  • Unknown's avatar

    I developed a advanced search function to search for custom post types by multiple post meta values. But it doesn’t work as I expected. Can anyone help me ? Here is the code I developed.

    function get_restaurants_by_area(){

    $searched_posts = null;
    $postType = ‘restaurant’;
    $location = $_GET[‘s’];
    $service = $_GET[‘service’];

    if($location != ” && $service != ” ){
    $args = array(
    ‘post_type’ => $postType,
    ‘cache_results’ => false,
    ‘meta_query’ => array(
    ‘relation’ => ‘AND’,
    array(
    ‘key’ => ‘location’,
    ‘value’ => $loctn ,
    ‘compare’ => ‘LIKE’
    ),
    array(
    ‘key’ => ‘available_services’,
    ‘value’ => $service ,
    ‘compare’ => ‘LIKE’
    )
    )
    );
    } elseif($location = ‘Location’ && $service != ” ) {
    $zip = $_GET[‘zip’];
    if($zip != ” && $service != ” ){
    $args = array(
    ‘post_type’ => $postType,
    ‘cache_results’ => false,
    ‘meta_query’ => array(
    ‘relation’ => ‘AND’,
    array(
    ‘key’ => ‘zip_code’,
    ‘value’ => $zip ,
    ‘compare’ => ‘LIKE’
    ),
    array(
    ‘key’ => ‘available_services’,
    ‘value’ => $service ,
    ‘compare’ => ‘LIKE’
    )
    )
    );
    }
    }
    $searched_posts = new WP_Query( $args );
    return $searched_posts;
    }

    And also I can’t get into search page without using “s” as an input name. Can we not use that and use something other than that ?

    Any help is greatly appreciate.

  • Unknown's avatar

    You are posting to the wrong support forum. We do not provide support at WordPress.COM for any installs of WordPress.ORG software. We provide support only for free hosted WordPress.COM blogs being hosted right here on this multiuser blogging platform.

    WordPress.com and WordPress.org have different log-ins and run different versions of themes with same names.
    http://support.wordpress.com/com-vs-org/

    If you don’t have a username account at WordPress.ORG click http://wordpress.org/support/ and register one on the top right hand corner of the page that opens, so you can post to the support forums there and receive advice from WordPress.ORG bloggers.

  • Unknown's avatar

    Thanks for your reply. I have a account on wordpress.org and couldn’t find the way to start a new topic. So, I started it here. And no worries, I could manage to fix the issue. Thanks

  • The topic ‘WordPress Search with multiple Custom Fields’ is closed to new replies.