26

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

  • 8
    Please be aware that you are expected to have researched the problem and made an attempt at solving it before posting a question. Had you not been brand new here I would have probably down-voted the question and moved on, rather than answer it. In the spirit of "Welcome to the Stack" this is your free-bee. Please take a look at How to Ask for future questions. – s_ha_dum May 11 '14 at 20:05
  • This one just made me lose some time because of the not accepted answer below. So I'm leaving here my 2 cents. He never answered, nor accepted the answer below. Why don't you just remove this questions while there are dozens of similar questions around here? – mircobabini Jul 21 at 13:02
44

What you are asking for is a meta_query

$args = array(
   '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.

  • 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 May 11 '14 at 20:35
8

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/

  • 1
    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 Mar 3 '17 at 10:11
7

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',
    ) );
    
  • 2
    Nice to know the short way with get_posts() – Andrew Welch Jan 26 '17 at 16:06
2

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/

  • May i know why this post_per_page value is -1? – Abhay Gawade Apr 27 '18 at 5:07
  • 1
    @AbhayGawade You can limit max number of results using that parameter, -1 means no limit. – Kush Jun 20 '18 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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