0

I have this WP_Query

<?php
        $args = array(
        'post_type' => 'banner',
        'posts_per_page' => 25 ,
        'meta_query' => array( 'relation' => 'AND',
        array('key' => 'ads_status','value' => '1'),
        array('key' => 'adsposition','value' => 'top'))
        );
        $slide = new WP_Query( $args );
    if ( $slide->have_posts() ) : while ( $slide->have_posts() ) : $slide->the_post();  
    $yourfile = get_post_meta(get_the_ID(), 'yourfile', true);  
    $adsposition = get_post_meta( get_the_ID(),'adsposition', true ); ?>
     <img src="<?php echo $yourfile; ?>" width="1350" height="515">
    <?php
endwhile; endif;
    wp_reset_query();
?>

I have tow meta box (ads_status,adsposition) and its saved good in wp-admin, but in home page not show any result, but when delete meta_query, Its work and get me result.

Where the error ??

1 Answer 1

1

Your query seems to be fine and it should work. It's not returning result probably because you are missing type parameter for ads_status field.

So your code should be.

<?php
$args = array(
  'post_type'   => 'banner',
  'posts_per_page' => 25,
  'meta_query'  => array(
    'relation'  => 'AND',
    array(
      'key'     => 'ads_status',
      'value'   => '1',
      'type'    => 'NUMERIC',
    ),
    array(
      'key'     => 'adsposition',
      'value'   => 'top',
    ),
  ),
);
$slide = new WP_Query( $args );
if ( $slide->have_posts() ) : while ( $slide->have_posts() ) : $slide->the_post();  
$yourfile = get_post_meta(get_the_ID(), 'yourfile', true);  
$adsposition = get_post_meta( get_the_ID(),'adsposition', true ); ?>
<img src="<?php echo $yourfile; ?>" width="1350" height="515">
<?php
endwhile; endif;
wp_reset_query();
?>

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.