0

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 2

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:

enter image description here

Refer this link More Knowledge

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.