WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Show posts that have custom meta box not empty (5 posts)

  1. gomymusic
    Member
    Posted 1 year ago #

    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.

  2. vtxyzzy
    Member
    Posted 1 year ago #

    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);
  3. gomymusic
    Member
    Posted 1 year ago #

    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.

  4. vtxyzzy
    Member
    Posted 1 year ago #

    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);
  5. gomymusic
    Member
    Posted 1 year ago #

    Thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic