简体   繁体   中英

Get custom post types posts using custom meta fileds value wordpress


$posts = new WP_Query( [
        'post_type'  => 'mock_test',
        'meta_query' => array(
        'key'     => 'selectedchapter',
        'value' => 15
        ),
    ] );

 echo "<pre>";
 print_r($posts);
 echo "</pre>";

I need to get posts which have selected chapter 2 in custom meta box. This WP_Query not working for me, I'm getting all the posts. see attached screenshot ( https://prnt.sc/vuzw6lhW44mR ) of metabox html structure.

This is a simple way to get post where the custom field key is 'color' and the custom field value is 'blue':

   $args = array(
       'post_type' => 'product',
        'meta_key' => 'color',
       'meta_value' => 'blue'
     );
    $wp_query = new WP_Query( $args );

Try these code

$args = array(  
        'post_type' => 'mock_test', 
    );

    $loop = new WP_Query( $args ); 
        
    while ( $loop->have_posts() ) : $loop->the_post(); 

        print the_title(); 
        the_excerpt(); 
        $meta_print_value=get_post_meta(get_the_ID(),'selectedchapter',true);
        echo $meta_print_value;

    endwhile;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM