WordPress custom fields are good for adding meta data to posts. These custom fields are not just for displaying data but can be used to sort and filter posts. You may be interested in making a form to filter posts by custom fields. To create custom fields search filters we use Below page template can be applied to any page where you want to display your search filters in right sidebar, on form submission it will show you filtered posts in primary area of the page. This page template can be customized to fit your requirements.WP_Query and specify a meta_query. For example we have a custom post type ‘vendor’, four custom fields country, state, zip and age and vendors are organized in categories. We may be interested to see vendors from certain category and state with certain age group. How do we do that? WordPress provides a nice way to create such meta data search filters. <?php /** * Template Name: Custom Search * * @author Tahir Yasin */ get_header(); $fields = array('country', 'state', 'zip', 'min_age', 'max_age'); foreach ($fields as $field) { if ($_REQUEST[$field] != '') { if ($field == 'min_age') { $meta_query[] = array( 'key' => $field, 'value' => $_REQUEST[$field], // This is OK, WP_Query will sanitize input! 'compare' => '>=', ); } elseif ($field == 'max_age') { $meta_query[] = array( 'key' => $field, 'value' => $_REQUEST[$field], // This is OK, WP_Query will sanitize input! 'compare' => '<=', ); } else // 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' => '=', ); } } $args = array( 'post_type' => 'vendor', 'category__in' => $_REQUEST['category'], 'posts_per_page' => -1, // -1 to display all results at once 'order' => 'ASC', 'meta_query' => $meta_query, ); $query = new WP_Query($args); ?> <div id="primary"> <div id="content" role="main"> <?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?> <div class="post"> <?php the_title(); ?> </div> <?php endwhile; //end of the loop endif; ?> </div><!-- #content --> </div><!-- #primary --> <div id="secondary" class="widget-area" role="complementary"> <form method="get"> <label for="country">Country:</label> <input type="text" name="country" value="" /> <label for="state">State: </label> <input type="text" name="state" value="" /> <label for="zip">Zip: </label> <input type="text" name="zip" value="" /> <label for="min_age">Min Age:</label> <input type="text" name="min_age" value="" /> <label for="max_age">Max Age: </label> <input type="text" name="max_age" value="" /> <label for="category">Category:</label><br /> Category 1<input type="checkbox" value="5" name="category[]" /><br /> Category 2<input type="checkbox" value="6" name="category[]" /><br /> Category 3<input type="checkbox" value="7" name="category[]" /><br /> Category 4<input type="checkbox" value="8" name="category[]" /><br /> <input type="submit" value="Search" /> </form> </div> <?php get_footer(); ?>
WordPress
WordPress search filters based on custom fields
WordPress custom fields are good for adding meta data to posts. These custom fields are not just for displaying data but can be used to sort and filter posts. Y
· 5 min read
Related posts
WordPress
Adding Custom Fields to WordPress User Profile and Add New User page
Adding custom profile fields in WordPress made easy through action hooks. .Most of the solutions available on internet tell you about adding custom fields on &#
WordPress
Script Logic Plugin
Introduction Script Logic is a WordPress plugin that lets you control on which pages scripts and style sheets load using WP’s conditional tags. This plug
WordPress
Multi Image Upload
Multi Image Upload adds a meta box to upload multiple images for posts and pages. You can enable it for custom post types also. Download: http://wordpress.org/p