I have the following code in template-home page which queries for a custom post type, called reviews. I have a Meta Box for the Custom Post Type where I can write the Author, Article Link, an excerpt to be displayed on the Home Page and a checkbox. If the checkbox is checked that post gets pulled on the Home Page.

        if(have_posts()) {
            while(have_posts()) {

                // store the page content for future use
                $home_content = get_the_content();

                if(has_post_thumbnail()) { ?>
                    <div class="rounded-image">
                        <?php the_post_thumbnail(); ?>
                <?php }

    $args = array('post_type' => 'reviews');
    $home_reviews_query = new WP_Query($args);

    if($home_reviews_query->have_posts()) {
        while($home_reviews_query->have_posts()) {

            // Retrieve Meta Box Options
            $checked_review = get_post_meta($post->ID, 'review_check', true);
            $review_author = esc_html(get_post_meta($post->ID, 'review_author', true));
            $review_excerpt = esc_html(get_post_meta($post->ID, 'review_excerpt', true));
            $review_content = get_the_content();

            if($checked_review && $review_content != '') { ?>
                    <?php if($review_excerpt != '') : ?>
                                echo $review_excerpt;
                    <?php elseif($review_excerpt == '') : ?>
                    <?php endif; ?>
                    <cite>- <?php echo $review_author; ?> -</cite>
            <?php }                     
        }// end while           
    } // end if


I would like that if there aren't any posts checked, I could display something else instead of the reviews, like the_content() for the Home Page, as backup in case someone forgets to check any reviews. I was thinking I could check if $checked_review is false for all posts and if that returned true it would then display some other bit of content. However I have no idea to properly do that.

I tried the following piece of code in the loop and after the loop but with no result.

if($checked_review == false) {
   echo $home_content;

I previously stored the Home Page the_content() in the $home_content variable. before I started the query for the Reviews Post Type.

Had a go with get_posts() as well but to no solution. Hopefully my post is not too confusing.

I'm not that good with PHP so any suggestions/push in the right direction would be greatly appreciated.


It isn't clear how you are getting $home_content but you will need two loops: one to get the home page content, however you are retrieving that, and one for your reviews content above. You will need to use wp_reset_postdata() after both loops.

  • after the get_header() I'm just doing a normal loop where I also make use of the post_thumbnail() and i'm also storing the page content into that variable. I'm using reset_post_data() before the second query. – aroundsix Sep 23 '15 at 21:55
  • Updated the first post to make it more clear. Apologize I didn't make it clear the first time. – aroundsix Sep 23 '15 at 22:07
  • Is $home_content empty? If not, can you just do: if($checked_review && $review_content != '') { // Output reviews } else { echo $home_content; } – Dave McCourt Sep 24 '15 at 8:55
  • The $home_content has some lorem ipsum. I had already tried what you suggested above but that outputs the $home_content for every other review that isn't checked. Also tried 'if($checked_review)' outside the loop and if there's even one review that isn't checked it outputs the $home_content regardless if there are any checked. I need a way to check all the reviews for that meta box value and if all of them are unchecked then display the $home_content just once. I feel like there's probably an easy way to do this and I'm just missing it. – aroundsix Sep 24 '15 at 12:15
  • What I'm not clear about is your set-up. You say you are using a custom template for this but what posts is the first loop pulling in? I'd suggest creating a home.php template and adjusting your first loop to pull in a post from a category called 'Home page'. Here is an example of something similar I use: pastebin.com/1HzbN0SB You probably need $checked_review to be an array $checked_review[] = get_post_meta($post->ID, 'review_check', true); so that it isn't replaced each time in the loop. Then out of the loop check if (sizeof($checked_review) > 0) { ... – Dave McCourt Sep 24 '15 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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