码迷,mamicode.com
首页 > 其他好文 > 详细

[转]How to query posts filtered by custom field values

时间:2015-03-04 20:42:15      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

Description

It is often necessary to query the database for a list of posts based on a custom field value. This guide will demonstrate how it is possible using the native WP functions

Requirements

Example 1

Compare with a single custom field value

In this example, we will find all posts that have a post_type of ‘event’ where the custom field ‘location’ is equal to ‘Melbourne’. The custom field ‘ location’ in this case could be a text field, radio button or select field (something that saves a single text value)

<?php 

// args
$args = array(
	‘numberposts‘ => -1,
	‘post_type‘ => ‘event‘,
	‘meta_key‘ => ‘location‘,
	‘meta_value‘ => ‘Melbourne‘
);

// get results
$the_query = new WP_Query( $args );

// The Loop
?>
<?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(). ?>

 

Example 2

Compare with multiple custom field values (text based values)

In this example, we will find all posts that have a post_type of ‘event’ where the custom field ‘location’ is equal to ‘Melbourne’ and the custom field ‘attendees’ is higher than 100. The custom field ‘ attendees’ in this case could be a number field, text field, radio button or select field (something that saves a single text value)

<?php 

// args
$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‘ => ‘>‘
		)
	)
);

// get results
$the_query = new WP_Query( $args );

// The Loop
?>
<?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(). ?>

Example 3

Compare with multiple custom field values (array based values)

In this example, we will find all posts that have a post_type of ‘event’ where the custom field ‘location’ is equal to ‘Melbourne’ or ‘Sydney’. The custom field ‘ location’ in this case could be a multi-select or checkbox field (something that saves a serialized array value)

<?php 

// args
$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‘
		)
	)
);

// get results
$the_query = new WP_Query( $args );

// The Loop
?>
<?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(). ?>

Example 4

Query based on repeater field values

In this example, we will find all posts that have a post_type of ‘event’ which have at least 1 row of data for the ‘images’ repeater field. The custom field ‘images’ in this case is a repeater field containing a sub field called ‘image’.

Due to the nature that the repeater field saves it’s data, it can be thought of that the value for ‘images’ is a number containing the number of rows. You can read more about the repeater field data here

<?php 

// args
$args = array(
    ‘numberposts‘ => -1,
    ‘post_type‘ => ‘event‘,
    ‘meta_query‘ => array(
        array(
            ‘key‘ => ‘images‘,
            ‘value‘ => 0,
            ‘type‘ => ‘NUMERIC‘,
            ‘compare‘ => ‘>‘
        )
    )
);

// get results
$the_query = new WP_Query( $args );

// The Loop
?>
<?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>
            <?php // load ‘images‘ repeater field data. Please see repeater field for code examples ?>
        </li>
    <?php endwhile; ?>
    </ul>
<?php endif; ?>

<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>

Example 5

Compare with sub custom field values

In this example, we will find all posts that have a post_type of ‘event’ where the custom field ‘images’ has a sub field ‘type’ with a value of ‘Speaker’. The custom field ‘images’ is a repeater field containing 2 sub fields; ‘image’ (image field) and ‘type’ (a select field containing choices such as ‘Speaking’, ‘Performance’, ‘Music’, ‘Dance’). This situation could be found in a blog that captures an event’s photographs. Each photograph for an event is loaded into the repeater field and a ‘type’ of image is selected in the same row.

For sub field querying to work, we need to remember that the row number is not known (where the image is of type ‘Speaker’). Therefore, we must use a LIKE clause in our SQL query to allow for a WILDCARD in the meta_key search. To do this, we create a custom filter to replace the standard ‘=’ with ‘LIKE’. You can see this below

<?php 

// custom filter to replace ‘=‘ with ‘LIKE‘
function my_posts_where( $where )
{
	$where = str_replace("meta_key = ‘images_%_type‘", "meta_key LIKE ‘images_%_type‘", $where);

	return $where;
}

add_filter(‘posts_where‘, ‘my_posts_where‘);

// args
$args = array(
	‘numberposts‘ => -1,
	‘post_type‘ => ‘event‘,
	‘meta_query‘ => array(
		array(
			‘key‘ => ‘images_%_type‘,
			‘value‘ => ‘type_1‘,
		)
	)
);

// get results
$the_query = new WP_Query( $args );

// The Loop
?>
<?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(). ?>

Notes

Please note that some plugins / themes will hook in and modify any WP_Query attributes. To avoid this potential conflict, you can add another setting to the $args array called ‘suppress_filters‘ => false


 



[转]How to query posts filtered by custom field values

标签:

原文地址:http://www.cnblogs.com/huangtailang/p/4314080.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!