In wordpress there are several ways to get posts. Here we are going to show how to retrieve posts from database with filter by custom fields. We will make use of the common get_posts function, WP_Query Object and pre_get_posts filter.
Get Posts without Custom Fields Filter
First lets see how to query all posts and display them in a list. Please note the functions setup_postdata() and wp_reset_postdata() are used to allow functions such as the_permalink() and the_title() to work as expected.
<?php $posts = get_posts(array( 'posts_per_page' => -1, 'post_type' => 'post' )); if( $posts ): ?> <ul> <?php foreach( $posts as $post ): setup_postdata( $post ); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li> <?php endforeach; ?> </ul> <?php wp_reset_postdata(); ?> <?php endif; ?>
Get Posts with Custom Fields Filter
get_posts function and WP_Query Object, both accept arguments to get posts by custom field values. Here we are going to show 2 way (basic and advanced) to query posts by custom fields.
Basic Example
Lets see how to get all posts where a custom field called ‘department’ has a value of ‘sales’.
$posts = get_posts(array( 'numberposts' => -1, 'post_type' => 'post', 'meta_key' => 'department', 'meta_value' => 'sales' ));
To get list of custom post types and filter by custom fields, we can use above method and change the value of ‘psot_type’. See example below:
$posts = get_posts(array( 'numberposts' => -1, 'post_type' => 'employee', 'meta_key' => 'department', 'meta_value' => 'sales' ));
In basic example, we can filter using single custom fields. To filter by multiple fields we need to use advanced way.
Advanced Example
Here we will prepare arguments to find all posts where a custom field called ‘departemnt’ has a value of either ‘sales’ or ‘accounts’ and another custom field called ‘salary’ has a value greater than 25000.
$posts = get_posts(array( 'numberposts' => -1, 'post_type' => 'post', 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'department', 'value' => array('sales', 'accounts'), 'compare' => 'IN', ), array( 'key' => 'salary', 'value' => '2500', 'compare' => '>', ), ), ));
More Examples
Multiple custom field values (text based values)
$args = array( 'numberposts' => -1, 'post_type' => 'event', 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'location', 'value' => 'Melbourne', 'compare' => '=' ), array( 'key' => 'attendees', 'value' => 100, 'type' => 'NUMERIC', 'compare' => '>' ) ) ); $the_query = new WP_Query( $args ); ?> <?php if( $the_query->have_posts() ): ?> <ul> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <li> <a href="<?php the_permalink(); ?>"> <img src="<?php the_field('event_thumbnail'); ?>" /> <?php the_title(); ?> </a> </li> <?php endwhile; ?> </ul> <?php endif; ?> <?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
Multiple custom field values (array based values)
<?php $args = array( 'numberposts' => -1, 'post_type' => 'event', 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'location', 'value' => 'Melbourne', 'compare' => 'LIKE' ), array( 'key' => 'location', 'value' => 'Sydney', 'compare' => 'LIKE' ) ) ); $the_query = new WP_Query( $args ); ?> <?php if( $the_query->have_posts() ): ?> <ul> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <li> <a href="<?php the_permalink(); ?>"> <img src="<?php the_field('event_thumbnail'); ?>" /> <?php the_title(); ?> </a> </li> <?php endwhile; ?> </ul> <?php endif; ?> <?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
Sub custom field values
<?php // filter function my_posts_where( $where ) { $where = str_replace("meta_key = 'locations_$", "meta_key LIKE 'locations_%", $where); return $where; } add_filter('posts_where', 'my_posts_where'); // vars $city = 'Melbourne'; // args $args = array( 'numberposts' => -1, 'post_type' => 'event', 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'locations_$_city', 'compare' => '=', 'value' => 'Melbourne', ), array( 'key' => 'locations_$_city', 'compare' => '=', 'value' => 'Sydney', ) ) ); // query $the_query = new WP_Query( $args ); ?> <?php if( $the_query->have_posts() ): ?> <ul> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li> <?php endwhile; ?> </ul> <?php endif; ?> <?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
Dynamic $_GET parameters
function my_pre_get_posts( $query ) { // do not modify queries in the admin if( is_admin() ) { return $query; } // only modify queries for 'event' post type if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'event' ) { // allow the url to alter the query if( isset($_GET['city']) ) { $query->set('meta_key', 'city'); $query->set('meta_value', $_GET['city']); } } // return return $query; } add_action('pre_get_posts', 'my_pre_get_posts');