Query about wp_query

I am new in WordPress. I would like to know more about wp_query. I have below code in my WordPress site. $metaquery = ar

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 ?

Share Improve this question asked Mar 13, 2020 at 2:13 FoysalFoysal 4451 gold badge5 silver badges16 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Basically 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 since WP_Query doesn't query all (post) meta by default.

You can find all the WP_Query parameters here and here for the meta_query (meta query clauses).

Additional Notes

These are additional details, which may be useful to you, in addition to the other answer.

  1. The meta_query parameter, or WordPress meta queries, are not only used with WP_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.

  2. 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 or Favorite Music in the 90's.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744665251a4586707.html

相关推荐

  • Query about wp_query

    I am new in WordPress. I would like to know more about wp_query. I have below code in my WordPress site. $metaquery = ar

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信