wp query – New WP_Query not isolating custom post types on front-page template

Question

This is the code for my front-page.php template

<?php
add_filter( 'genesis_markup_site-inner_output', '__return_false' );
add_filter( 'genesis_markup_content_output', '__return_false' );
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );

//* Remove page titles site wide (posts & pages) (requires HTML5 theme support)
remove_action( 'genesis_entry_header', 'genesis_do_post_title' );

//* Remove the entry header markup (requires HTML5 theme support)
remove_action( 'genesis_entry_header', 'genesis_entry_header_markup_open', 5 );
remove_action( 'genesis_entry_header', 'genesis_entry_header_markup_close', 15 );

// Execute custom home page. If no widgets active, then loop
remove_action( 'genesis_before_header', 'vm_utility_bar' );
add_action( 'genesis_meta', 'vm_custom_home_loop' );

function vm_custom_home_loop() {
    global $post;
    remove_action( 'genesis_loop', 'genesis_do_loop' );
    add_action( 'genesis_before_content', 'vm_home' );
}

// Home Top Gallery section

function vm_home() {
    echo '<main class="content">';
        vm_home_do_top();
        vm_home_do_bottom();
    echo '</main>';
}


function vm_home_do_top() {
    echo '<section id="home-top"><div class="home-top clearfix">';
    echo '<div class="home-top-left two-thirds first">';
    if ( function_exists( 'soliloquy' ) ) {
      soliloquy( 'rotating-videos', 'slug' );
     } else { if (is_active_sidebar( 'home-video' )) {
            genesis_widget_area( 'home-video', array(
                'before' => '<aside class="home-video video-container">',
                'after' => '</aside>',
            ) );
        }
        }
    echo '</div><!--end home-top-left-->';
    echo '<div class = "home-top-right one-third">';
    if ( is_active_sidebar( 'home-right' ) ) {
        genesis_widget_area( 'home-right', array(
            'before' => '<aside class="home-right">',
            'after' => '</aside>',
        ) );
    }
    echo '</div><!--end home-top-right-->';
    echo '</div><!--end home-top --></section><!-- end home-top -->';
}

// Home posts section

function vm_home_do_bottom() {
    echo '<section id="home-bottom"><div class="home-bottom clearfix">';
        echo '<aside class="home-bottom-left widget-area one-third first">';
        echo '<h4>Recent Posts: </h4>';
        echo '<ul class="home-posts-list">';
        wp_get_archives( 'type=postbypost&limit=100' );
        echo '</ul>';
        echo '</aside>';
            echo '<aside class="home-bottom-right widget-area two-thirds last">';
                wp_reset_query();
                $args = ( array( 'post_type' => 'shows', 'paged' => $paged ) );
                $vmQuery1 = new WP_Query( $args );
                while ( $vmQuery1->have_posts() ) : $vmQuery1->the_post();
                    $postID = get_the_ID();
                    $postDate = get_the_date('', $postID);
                    $postType = get_post_type( $postID );
                    echo '<p>The $postID is ' . $postID . ' The $postDate is ' . $postDate . ' The $postType is ' . $postType . '</p>';
                        the_title();
                        echo '<div class="entry-content">';
                        the_content();
                        echo '</div>';
                endwhile;
                wp_reset_postdata();
            echo '</aside>';
            //) );
    echo '</div></section><!-- end home-bottom -->';
}

genesis();

The echo of the post type, date and ID is strictly for troubleshooting purposes.

The query for the custom post type is returning all post types, including the post type shows. When I put that same query into a template of its own, it works properly and only returns shows.

I must be missing something obvious and I’m hoping someone here can explain to me how to isolate custom post types only within an active query for the front page. It has to be possible.

Thanks in advance for helpful contributions.

Addendum:
I just noticed that in my query that is returning all post types on the site (and shouldn’t be), it is identifying the custom post type as ‘post’ and not ‘shows’, even though it has the post ID and post date correct. It is not returning the title or any of the content either for shows.

Yet, the same query on archive-shows.php returns only shows so, obviously on a template other than front-page.php, the code knows what to look for and how to process it with my query.

Addendum 2:
I’ve gotten it to work. The code above was retrieving everything BUT the custom post type on front-page.php. As I’ve noted, the same query on archive-stories.php worked as expected.

These are the code changes I made to my front-page.php template.

At the top of the page, I added this function:

add_action( 'pre_get_posts', 'vm_get_shows' );
function vm_get_shows ( $query ) {
    $query->set('post_type', array( 'post', 'page', 'nav_menu_item', 'shows' ) );
}

The query section was modified thus:

echo '<aside class="home-bottom-right widget-area two-thirds last">';
   wp_reset_query();
   $args = array( 'post_type' => 'shows' );
   $vmQuery1 = new WP_Query( $args );
   if ( $vmQuery1->have_posts() ) : while ( $vmQuery1->have_posts() ) : $vmQuery1->the_post();
      $postID = get_the_ID();
      $postDate = get_the_date('', $postID);
      $postType = get_post_type( $postID );
      $postTitle = get_the_title($postID);
      if ( $postType == 'shows' ) {
        echo '<p>The $postID is ' . $postID . ' The $postDate is ' . $postDate . ' The $postType is ' . $postType . '</p>';
        the_title();
        echo '<div class="entry-content">';
          the_content();
        echo '</div>';
      }
      endwhile;
      endif;
      wp_reset_postdata();
echo '</aside>';

So now my question is:
Why did I have to redundantly check to ensure that the postType was shows with a second if statement within the query? Shouldn’t the arg of post_type=>’shows’ handled that on the front-page.php template?

0
Marj Wyatt 6 years 2014-12-13T01:08:38-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse