50

I would like to list all posts that have a key of cp_annonceur with the value professionnel.

1

6 Answers 6

81

What you are asking for is a meta_query

$args = array(
   'meta_key' => 'custom-meta-key',
   'meta_query' => array(
       array(
           'key' => 'cp_annonceur',
           'value' => 'professionnel',
           'compare' => '=',
       )
   )
);
$query = new WP_Query($args);

All of the information you need is in the Codex.

6
  • 3
    @Beginner : if this solved the problem please mark it "Accepted". Look for the check mark near the vote arrows on the left.
    – s_ha_dum
    Commented May 11, 2014 at 20:35
  • 1
    For some reason using new WP_Query($args) and then calling get_posts doesn't work for me. Directly passing the $args array to the get_posts function as an argument works however.
    – Kunal
    Commented May 5, 2020 at 21:24
  • @Kunal You don't need to call get_posts. Use the code above, and get the posts like so: $query->posts. It should return an array. Don't forget to read the link in the comment, as there is something missing in the code: $args = array( 'post_type' => 'your-cpt', 'meta_key' => 'your-meta', 'meta_query' => array( array( 'key' => 'cp_annonceur', 'value' => 'professionnel', 'compare' => '=', ) ) ); $query = new WP_Query($args);
    – BonisTech
    Commented Jan 14, 2021 at 0:32
  • @s_ha_dum minor syntax correction - missing , after 'custom-meta-key'. Would have editied, but it's not possible to make a 1 character edit...
    – Steven
    Commented Apr 29, 2021 at 12:57
  • What's the difference between meta_key and key ? Commented Jan 14, 2022 at 11:20
16

There are two ways to do that:

  1. Intercept the main query on pre_get_posts:

    add_action( 'pre_get_posts', function( $query )
    {
        // only handle the main query
        if ( ! $query->is_main_query() )
            return;
    
        $query->set( 'meta_key',   'cp_annonceur' );
        $query->set( 'meta_value', 'professionnel' );
    } );
    
  2. Add an additional query

    $second_loop = get_posts( array(
        'meta_key'   => 'cp_annonceur',
        'meta_value' => 'professionnel',
    ) );
    

A more throughout example can be found in this answer.

3
  • 2
    Nice to know the short way with get_posts() Commented Jan 26, 2017 at 16:06
  • How do you do this with multiple meta ket/values ?
    – G-J
    Commented Apr 9, 2020 at 22:57
  • @G-J you might want to take a look at this example.
    – kaiser
    Commented Apr 15, 2020 at 15:18
12

I used custom select (might be better performance)

$posts = $wpdb->get_results("SELECT * FROM $wpdb->postmeta
WHERE meta_key = 'cp_annonceur' AND  meta_value = 'professionnel' LIMIT 1", ARRAY_A);

Inspired from https://tommcfarlin.com/get-post-id-by-meta-value/

3
  • 6
    It might have better performances, but it throws away the whole idea of having Wordpress functions to search (and cache) data. And, also, what will happen if WP decides to change the table structure? :)
    – Erenor Paz
    Commented Mar 3, 2017 at 10:11
  • @ErenorPaz I understand what you are saying but this way would make it easy if you had multiple meta key/values as a criteria... Is there an official way to handle multiple criteria?
    – G-J
    Commented Apr 9, 2020 at 22:59
  • 2
    You mean something like WHERE metatable1.meta_key = 'cp_annonceur' AND metatable1.meta_value = 'professionnel' AND metatable2.meta_key = 'cp_other_meta' AND metatable2.meta_value = 'other_value'? (Note that i suppose we do a join on the same posts_meta table using two names metatable1 and metatable2). This can be achieved adding field meta_query (as an array) to the query. Take a look at: developer.wordpress.org/reference/classes/wp_query/… and go to paragraph "‘orderby’ with multiple ‘meta_key’s"
    – Erenor Paz
    Commented Apr 10, 2020 at 11:19
8

We can get the desired result with Meta query of the WordPress :

// the meta_key 'diplay_on_homepage' with the meta_value 'true'
$cc_args = array(
    'posts_per_page'   => -1,
    'post_type'        => 'post',
    'meta_key'         => 'cp_annonceur',
    'meta_value'       => 'professionnel'
);
$cc_query = new WP_Query( $cc_args );

For more detailed guide regarding meta query follow this blog : http://www.codecanal.com/get-posts-meta-values/

3
  • May i know why this post_per_page value is -1?
    – Abhay
    Commented Apr 27, 2018 at 5:07
  • 1
    @AbhayGawade You can limit max number of results using that parameter, -1 means no limit.
    – Kush
    Commented Jun 20, 2018 at 17:49
  • 1
    Thanks man works fine. :)
    – Arman H
    Commented Feb 23, 2022 at 6:05
5

You can easily grab the posts using WordPress's built-in function called get_posts You can find more details about get_posts here https://developer.wordpress.org/reference/functions/get_posts/

$posts = get_posts(array(
    'numberposts'   => -1,
    'post_type'     => 'post',
    'meta_key'      => 'cp_annonceur',
    'meta_value'    => 'professionnel'
));
1

Try this, It's working for me

global $wpdb;
$meta_key = '_request_body';

$data = $wpdb->get_results($wpdb->prepare( "SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key) , ARRAY_N  );

$result = [];
foreach($data as $array){
    $result[] = $array[0];
}

print_r($result);
2
  • That would list the distinct possible values, which isn't quite what the question asks for.
    – Rup
    Commented Jan 20, 2021 at 16:19
  • You should never access the database for such fundamental WP things as getting posts. You should always use abstracted WP-API which does all the heavy lifting for you and ensures your code will work if anything is changed under the hood. Commented Nov 18, 2022 at 20:03

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.