WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Show posts that have custom meta box not empty

[Resolved] Show posts that have custom meta box not empty

  • Hey,

    I need a little help.
    I have created custom post type “portfolio” with custom meta box “testimonials”. How can I display only 1 random “portfolio” post that have meta box “testimonials” not empty?

    Thanks a lot.

Viewing 4 replies - 1 through 4 (of 4 total)
  • I believe this set of arguments will do what you want:

    $post_type = 'portfolio';
    $meta_key = 'testimonials';
    $args = array(
       'post_type' => $post_type,
       'posts_per_page' => 1,
       'ignore_sticky_posts' => 1,
       'orderby' => 'rand',
       'meta_key' => $meta_key,
    );
    query_posts($args);

    Thanks for reply.
    In this case will display a random portfolio post, whether or not the testimonial field is completed, and I want to display one of those portfolio posts who have completed testimonial field.
    To be more clear, I want to list 1 random testimonial, that is a custom meta box of portfolio post, only if the field is completed.

    Thanks.

    If you are saying that you have posts where the ‘testimonial’ field is present but empty, you should be able to use this:

    $post_type = 'portfolio';
    $meta_key = 'testimonials';
    $args = array(
       'post_type' => $post_type,
       'posts_per_page' => 1,
       'ignore_sticky_posts' => 1,
       'orderby' => 'rand',
       'meta_key' => $meta_key,
       'meta_compare' => '>',
       'meta_value' => ' ',
    );
    query_posts($args);

    Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] Show posts that have custom meta box not empty’ is closed to new replies.
Skip to toolbar