Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have, hopefully easy question. I have my query here, which is using ACF

<?php query_posts(array('post_type' => 'our-clients-list', 'posts_per_page' => 1, 'order' => 'DSC', 'orderby' => 'rand','paged'=> $paged)); ?>

             <?php while(have_posts()) : the_post(); ?>

                       <?php the_field('testimonial_'); ?>

             <?php endwhile; ?>
             <?php wp_reset_query();?>

So there is a field in my custom post type called 'testimonial_' I would like to only execute query when custom field is not empty so !='' but not sure how to do that. Anyone can help or give me some hint?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

I think this should work, but I am not 100% certain on the advanced meta query for "not" a null string. That isn't normally how meta queries are used. As such, I have left the set_transient line commented out. I just noticed that you are trying to pull 1 random post, so you might not want to use the Transients API at all, but I think it would still be a good idea just with a shorter time limit, so I have the transient set to store for 1 hour. If not, you can always extract the query parts.

// Get any existing copy of our transient data
if ( false === ( $custom_testimonials = get_transient( 'custom_testimonials' ) ) ) {
    // It wasn't there, so regenerate the data and save the transient

   // params for our query

   array(); ?


    $args = array(
        'post_type' => 'our-clients-list'
       'posts_per_page'  => 1,
       'orderby' => 'rand'
       'meta_key'        => '_featured',
       'meta_value'      => 'yes',
        'meta_query' => array(
            array(
                'key' => 'testimonial_',
                'value' => '',
                'compare' => '!='
            )
        )
    );

    // The Query
    $custom_testimonials = new WP_Query( $args );

    // store the transient - uncomment when sure the query is working (stores for 1 hour)
    // set_transient( 'custom_testimonials', $custom_testimonials, 60*60*1 );

}

// Use the data like you would have normally...

// The Loop
if ( $custom_testimonials ) :

    echo '<ul class="testimonial">';

    while ( $custom_testimonials->have_posts() ) :
        $custom_testimonials->the_post();
        echo '<li>' . get_the_title() . '</li>';
    endwhile;

    echo '</ul>';

else :

echo 'No testimonials found.';

endif;

/* Restore original Post Data
 * NB: Because we are using new WP_Query we aren't stomping on the
 * original $wp_query and it does not need to be reset.
*/
wp_reset_postdata();

Excellent reference on Advanced Meta Queries

share|improve this answer
    
Work a charm, one small alteration 'compare' => '!=' instead of 'compare' => 'NOT LIKE', Thank you very much –  jmysona Feb 3 '13 at 18:31
    
Glad it helped. I've adjusted the code for future reference. –  helgatheviking Feb 3 '13 at 19:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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