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的更多相关文章

  1. redmine computed custom field formula tips

    项目中要用到Computed custom field插件,公式不知道怎么写,查了些资料,记录在这里. 1.http://apidock.com/ruby/Time/strftime 查看ruby的字 ...

  2. JIRA Plugin Development——Configurable Custom Field Plugin

    关于JIRA Plugin开发的中文资料相当少,这可能还是由于JIRA Plugin开发在国内比较小众的原因吧,下面介绍下自己的一个JIRA Plugin开发的详细过程. 业务需求 创建JIRA IS ...

  3. Add custom field in Material Master

    1.Add fields in the Append Structure of table MARA. 2.Configure SPRO IMG -> Logistics General -&g ...

  4. SharePoint Development - Custom Field using Visual Studio 2010 based SharePoint 2010

    博客地址 http://blog.csdn.net/foxdave 自定义列表的时候有时候需要自定义一些字段来更好地实现列表的功能,本文讲述自定义字段的一般步骤 打开Visual Studio,我们还 ...

  5. Query DSL for elasticsearch Query

    Query DSL Query DSL (资料来自: http://www.elasticsearch.cn/guide/reference/query-dsl/) http://elasticsea ...

  6. Yii的学习(3)--查询生成器 (Query Builder)

    原文地址:http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder 不过原文是英文的,Yii的官网没有翻译这一章,自己就尝 ...

  7. yii Query Builder (yii 查询构造器) 官方指南翻译

    /**** Query Builder translated by php攻城师 http://blog.csdn.net/phpgcs Preparing Query Builder 准备 Quer ...

  8. FluentData -Micro ORM with a fluent API that makes it simple to query a database 【MYSQL】

    官方地址:http://fluentdata.codeplex.com/documentation MYSQL: MySQL through the MySQL Connector .NET driv ...

  9. 数据库之Query Builder

    Yii的查询构造器提供了一个用面向对象的方法来构造SQL语句.他让开发人员可以用类的方法,属性来作为SQL语句的一部分.然后把不同部分组装到一个正确的SQL语句中,调用DAO的方法来执行.下面的例子演 ...

随机推荐

  1. hdu 2197 推公式

    题意:由0和1组成的串中,不能表示为由几个相同的较小的串连接成的串,称为本原串,有多少个长为n(n<=100000000)的本原串?答案mod2008.例如,100100不是本原串,因为他是由两 ...

  2. hdu 2216 bfs

    题目大意:两个东西朝相同方向移动Sample Input4 4XXXX.Z...XS.XXXX4 4XXXX.Z...X.SXXXX4 4XXXX.ZX..XS.XXXXSample Output11 ...

  3. java的反射机制(第二篇)

    本文转载自:http://c.biancheng.net/cpp/html/1781.html 要理解RTTI在Java中的工作原理,首先必须知道类型信息在运行时是如何表示的,这项工作是由“Class ...

  4. Codeforces Round #356 (Div. 2) A. Bear and Five Cards 水题

    A. Bear and Five Cards 题目连接: http://www.codeforces.com/contest/680/problem/A Description A little be ...

  5. hdoj 5122 K.Bro Sorting 贪心

    K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Tot ...

  6. Codeforces Round #294 (Div. 2)A - A and B and Chess 水题

    A. A and B and Chess time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. UVALive 5058 Counting BST 数学

    B - Counting BST Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit S ...

  8. HDU step by step

    section 1 不解释~ section 2 1.2.1 a+b coming #include<stdio.h> long long z,x,y; int main( ) { whi ...

  9. visio2013 激活工具,仅供交流学习

    visio2013激活软件 环境是 win7, 64 bit 装了 visio 2013 , 可以却不能用它来画图,在网上找了一些破解工具,大都不能解决问题.网上不靠谱的广告型文章太多了 所幸,终于找 ...

  10. Go语言Web框架gwk介绍 (二)

    HttpResult 凡是实现了HttpResult接口的对象,都可以作为gwk返回Web客户端的内容.HttpResult接口定义非常简单,只有一个方法: type HttpResult inter ...