skip to Main Content

$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.

2

Answers


  1. 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 );
    
    Login or Signup to reply.
  2. 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;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search