admin管理员组文章数量:1390921
I am new in WordPress. I would like to know more about wp_query. I have below code in my WordPress site.
$metaquery = array(
'relation' => 'AND',
array(
'relation' => 'AND',
array(
'key' => 'idonate_donor_bloodgroup',
'value' => sanitize_text_field( isset( $_GET['bloodgroup'] ) ? $_GET['bloodgroup'] : '' ),
'compare' => '='
),
array(
'key' => 'idonate_donor_availability',
'value' => sanitize_text_field( isset( $_GET['availability'] ) ? $_GET['availability'] : '' ),
'compare' => '='
),
),
array(
'relation' => 'OR',
array(
'key' => 'idonate_donor_country',
'value' => sanitize_text_field( isset( $_GET['country'] ) ? $_GET['country'] : '' ),
'compare' => '='
),
array(
'key' => 'idonate_donor_state',
'value' => esc_attr( isset( $_GET['state'] ) ? $_GET['state'] : '' ),
'compare' => '='
),
array(
'key' => 'idonate_donor_city',
'value' => esc_attr( isset( $_GET['city'] ) ? $_GET['city'] : '' ),
'compare' => '='
),
)
);
I would like to know What is the purpose of the key
here ? Why should I use it ? How to generate this key
? How to use this key
? Can I use anything as key
?
I am new in WordPress. I would like to know more about wp_query. I have below code in my WordPress site.
$metaquery = array(
'relation' => 'AND',
array(
'relation' => 'AND',
array(
'key' => 'idonate_donor_bloodgroup',
'value' => sanitize_text_field( isset( $_GET['bloodgroup'] ) ? $_GET['bloodgroup'] : '' ),
'compare' => '='
),
array(
'key' => 'idonate_donor_availability',
'value' => sanitize_text_field( isset( $_GET['availability'] ) ? $_GET['availability'] : '' ),
'compare' => '='
),
),
array(
'relation' => 'OR',
array(
'key' => 'idonate_donor_country',
'value' => sanitize_text_field( isset( $_GET['country'] ) ? $_GET['country'] : '' ),
'compare' => '='
),
array(
'key' => 'idonate_donor_state',
'value' => esc_attr( isset( $_GET['state'] ) ? $_GET['state'] : '' ),
'compare' => '='
),
array(
'key' => 'idonate_donor_city',
'value' => esc_attr( isset( $_GET['city'] ) ? $_GET['city'] : '' ),
'compare' => '='
),
)
);
I would like to know What is the purpose of the key
here ? Why should I use it ? How to generate this key
? How to use this key
? Can I use anything as key
?
2 Answers
Reset to default 1Basically meta_query parameter of WP_Query allows you to search WordPress posts / pages / custom post types by their meta data and sort the result.
In this post I assume that you already have basic knowledge how to work with WP_Query class in WordPress. Before I begin, I want to show you some very simple examples. The similar examples you can find in WordPress Codex.
As you know all the posts have the metadata you can populate in "Custom fields" metabox (the metabox by the way can be hidden). So, for example, if you want to get a post with meta key show_on_homepage and meta value on, you can do it the following way:
$rd_args = array(
'meta_key' => 'show_on_homepage',
'meta_value' => 'on'
);
$rd_query = new WP_Query( $rd_args );
- Query Posts by a Meta Value
The simple example below allows you to get all the posts with a specific custom field value. Let’s just get all the posts with custom field name "color" and custom field value white.
// the meta_key 'color' with the meta_value 'white'
$rd_args = array(
'meta_query' => array(
array(
'key' => 'color',
'value' => 'white'
)
)
);
$rd_query = new WP_Query( $rd_args );
If you look at the post edit page (in admin area) in any post which matches the query, you will see the following in the "Custom Fields" section:
Refer this link More Knowledge
Note (to other readers): If you are looking for a beginner-level guide on WordPress (post) meta queries, have a look at the other answer — or the linked article (written by Misha) — and you may also want to read this article which is a beginner-friendly introduction to custom fields or metadata which what meta_query
(WordPress meta queries) is really for.
Original Answer
This was in reply to the "What is the purpose of the key
here ? Why should I use it ?":
That
key
parameter is the meta key (or the custom field's name — think of it like post slugs). And without it, WordPress/MySQL won't know what meta should be queried for sinceWP_Query
doesn't query all (post) meta by default.You can find all the
WP_Query
parameters here and here for themeta_query
(meta query clauses).
Additional Notes
These are additional details, which may be useful to you, in addition to the other answer.
The
meta_query
parameter, or WordPress meta queries, are not only used withWP_Query
or for searching/filtering posts, but also for searching/filtering terms like the default/built-in Category (category
) taxonomy, comments, users, and sites in a Multisite network.However, the default Custom Fields editor/metabox is only available for posts. For terms, users, etc., you can code your own metabox or use a plugin like Advanced Custom Fields.
In reply to the "Can I use anything as key ?":
Yes, basically.
But technically, a meta key is limited to a maximum of 255 characters and plugins/themes (and you) can use filter hooks to allow/disallow certain characters, limit the key length, etc.
And despite what I said in the original answer ("think of it like post slugs"), meta key can actually be like a post title which contains spaces as in
Continue Reading
orFavorite Music in the 90's
.
本文标签: Query about wpquery
版权声明:本文标题:Query about wp_query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744665251a2618489.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论