首页 技术 正文
技术 2022年11月16日
0 收藏 1000 点赞 3,546 浏览 5394 个字

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(); ?>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><?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(); ?>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><?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(); ?>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><?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(); ?>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><?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(); ?>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><?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

来源: <http://www.advancedcustomfields.com/resources/how-to-query-posts-filtered-by-custom-field-values/>http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
来自为知笔记(Wiz)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291