WordPress.org

Ready to get started?Download WordPress

Forums

filter search query post meta (4 posts)

  1. crawpdx
    Member
    Posted 2 years ago #

    [ Moderator note: please wrap code in backticks or use the code button. ]

    Hey Folks- I'm looking for way to filter search results.

    This is what I have so far

    - a custom post type of "doctors"
    - in each "doctor" post, there are numerous custom fields (male or female, location, specialty, ect)

    I want the search form to filter these fields- for instance, display all the docs in a certain location that are female.

    Here is the code than I am using that is working on one field only (I click for female doctors in tigard and I get all doctors in tigard).

    <?php
    $args = array(
    'post_type' => 'Doctors',
        'posts_per_page' => 1000,
        'orderby' => 'meta_value_num',
        'order' => 'ASC',
        'meta_query' => array(
                    'relation' => 'AND',
            array(
                    'key' => 'gender',
                    'value' => 'yes',
    
            ),
            array(
                    'key' => 'age-groups-served',
                    'value' => 'yes',
    
            ),
             array(
                    'key' => 'languages',
                    'value' => 'yes',
    
            ),
             array(
                    'key' => 'location',
                    'value' => 'yes',
    
            ),
              array(
                    'key' => 'specialities',
                    'value' => 'yes',
    
            ),
              array(
                    'key' => 'tests',
                    'value' => 'yes',
    
            )
         )
    );
    $query = new WP_Query( $args );
    
    if (have_posts()) : while (have_posts()) : the_post();
    
    ?>

    FYI- I am using the "search everything" plugin so I cna display the custom fields in search.

    Here is the link if anyone is interested

    http://western.art4orm-dev2.com/staff-directory/

    Thanks for any help.

  2. toocoolone
    Member
    Posted 2 years ago #

    Make sure you are using 'post_type' => 'slug' and not the human readable form of your custom post type. I'm assuming you are but that's often an issue. Anyway...

    Don't forget the 'meta_key' => 'SORTFIELD', element. This tells WP_Query which meta_key value to sort. Change SORTFIELD to whatever the key's name is.

    The use of 'meta_query' requires WP 3.1 or better so make sure you've got the latest updates on your site. You can check the Codex for details on WP_Query.

    I removed the 'relation' = 'AND', and added 'compare' => 'LIKE', to your elements. This will not ignore fields that are left blank, however. You should write additional PHP to omit fields that are blank so they are ignored. Otherwise you likely will not get any hits if blank fields are left on the form and not in the database. (You never know what people will put into your perfectly planned program that will bring it crashing down!!)

    All your values are yes. I don't get that. I'm assuming you want to take form input and match it against the values in the database. That is what my solution will do...if they select a male doctor they will get only matches of the male gender. Be careful here though! If your choices are female and male you'll get everything in the database as they will both match a LIKE statement as below. Consider recording male as M and female as F in your database...

    You might try this to do what you're doing above:

    $args = array(
    	'post_type' => 'Doctors',
    	'posts_per_page' => 1000,
    	'meta_key' => 'SORTFIELD', // The name of the metakey to orderby
    	'orderby' => 'meta_value_num',
    	'order' => 'ASC',
    	'meta_query' => array(     // This is the problem area! (See below)
    		array(
    			'key' => 'gender',
    			'value' => 'yes',
    			'compare' => 'LIKE',
    		),
    		array(
    			'key' => 'age-groups-served',
    			'value' => 'yes',
    			'compare' => 'LIKE',
    		),
    		array(
    			'key' => 'languages',
    			'value' => 'yes',
    			'compare' => 'LIKE',
    		),
    		array(
    			'key' => 'location',
    			'value' => 'yes',
    			'compare' => 'LIKE',
    		),
    		array(
    			'key' => 'specialities',
    			'value' => 'yes',
    			'compare' => 'LIKE',
    		),
    		array(
    			'key' => 'tests',
    			'value' => 'yes',
    			'compare' => 'LIKE',
    		),
    	),
    );

    You could do something like this:

    // Only check these form fields (change the list as needed...)
    $fields = array( 'gender', 'age-groups-served', 'languages', 'location', 'specialties', 'tests' );
    foreach( $fields as $field ) {
    	if( $_REQUEST[$field] != '' ) {
    		// We have something to match, otherwise ignore the field...
    		$meta_query[] = array(
    			'key' => $field,
    			'value' => $_REQUEST[$field],  // This is OK, WP_Query will sanitize input!
    			'compare' => 'LIKE',
    		);
    	}
    }

    And change your $args to this:

    $args = array(
    	'post_type' => 'Doctors',
    	'posts_per_page' => 1000,
    	'meta_key' => 'SORTFIELD', // The name of the metakey to orderby
    	'orderby' => 'meta_value_num',
    	'order' => 'ASC',
    	'meta_query' => $meta_query,
    );
  3. crawpdx
    Member
    Posted 2 years ago #

    Thanks for your help. I'm going to plug this in this morning and let you know know how it goes.

    Thanks again for your generosity!

  4. crawpdx
    Member
    Posted 2 years ago #

    [ Moderator note: please wrap code in backticks or use the code button. ]

    toocoolone- thanks again for the code. It seems that I'm doing something wrong b/c now it's only showing me one instance of a male doctor at a location that I know has at least two male doctors.

    Would you mind posting the entire piece of code from top to bottom?

    Here is my current search.php code that I am using that us delivering the above mentioned result.

    <?php get_header(); ?>
    
    <div id="searchleft">
    
    <?php
    
    $args = array(
    	'post_type' => 'doctor',
    	'posts_per_page' => 1000,
    	'meta_key' => 'wpcf-location', // The name of the metakey to orderby
    	'orderby' => 'meta_value_num',
    	'order' => 'ASC',
    	'meta_query' => array(     // This is the problem area! (See below)
    		array(
    			'key' => 'gender',
    			'value' => 'yes',
    			'compare' => 'LIKE',
    		),
    		array(
    			'key' => 'age-groups-served',
    			'value' => 'yes',
    			'compare' => 'LIKE',
    		),
    		array(
    			'key' => 'languages',
    			'value' => 'yes',
    			'compare' => 'LIKE',
    		),
    		array(
    			'key' => 'location',
    			'value' => 'yes',
    			'compare' => 'LIKE',
    		),
    		array(
    			'key' => 'specialities',
    			'value' => 'yes',
    			'compare' => 'LIKE',
    		),
    		array(
    			'key' => 'tests',
    			'value' => 'yes',
    			'compare' => 'LIKE',
    		),
    	),
    );
    
    $query = new WP_Query( $args );
    
    if (have_posts()) : while (have_posts()) : the_post();
    
    ?>
    
    <h1 class="title"><a>" rel="bookmark" 
    
    title="Permanent Link to <?php the_title_attribute(); ?>">
    
    <?php the_title(); ?></a></h1>
    
    <?php endwhile; ?>
    
    <?php endif; ?>
    
    <!-- close searchleft-->
    </div>
    
    <div id="datasidebar"></div>
    
    <div style="clear:both;"></div>
    
    <?php get_footer(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic