Support

Account

Home Forums General Issues Issue with WP meta query

Solving

Issue with WP meta query

  • I have been trying this section of code without success for use on a select field. I also tried switching it to a check box field too and no change. When I remove the meta value the posts display correctly but I can not get it to query the posts with the value. Any ideas?

    <?php
    	       $args = array(
    			 'numberposts' => 2,
    			 'post_type' => 'ctas',
    			 'meta_key' => 'cta_location',
    			 'meta_value' => 'about'
    			 
    	       );
    	       $the_query = new WP_Query( $args );
    	       if( $the_query->have_posts() ): 
    		        while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    			       <div class="widget">
    				       <?php the_content(); ?>
    			       </div>
    		       <?php endwhile;   
    		    endif;
    	       wp_reset_query(); 
    	        } ?>
  • Hi,

    You can use the following codes as well,

    <?php
    $posts = get_posts(array(
    	'numberposts' => 2,
    	'post_type' => 'ctas',
    	'meta_key' => 'cta_location',
            'meta_value' => 'about'
    ));  
    ?>
     
    foreach ( $posts as $post ) : setup_postdata( $post ); ?>
            <div class="widget">
    	     <?php the_content(); ?>
    	</div>
    <?php endforeach; wp_reset_postdata();?>

    I hope that helps,

  • Thanks reardestani but unfortunately that made no difference. I really can’t understand where I am going wrong with this one. Just can’t filter posts by the meta value using select or checkboxes. Going to try switching it to true/false buttons and see if I can get it to work but bit disappointing it doesn’t seem to work with the select and check boxes.

  • Hi @Altered Mode

    It may be as simple as a spelling mistake in your meta_key or meta_value. Double check that the field name is correct, and the value saved in the DB is exactly what you have.

    Thanks
    E

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Issue with WP meta query’ is closed to new replies.