-1

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?

1

1 Answer 1

0

You can use a conditional tag with the pre_get_posts action hook like this from your functions file.

add_action( 'pre_get_posts', 'vm_get_shows' );
function vm_get_shows ( $query ) {
if ( !is_admin() && $query->is_main_query() && is_home() ) {
    $query->set('post_type', 'shows' );
    }
}
7
  • I actually had just that for a while. It did not solve the problem that I was having. I tried it within front-page.php as well as in a template part. I'm really wondering if there is something about the operation of front-page.php that caused the query to fail. As well, without the redundant check to ensure I had a show, it just wasn't working. I was of the belief that the query args should have isolated the post type I wanted for that section of the page's output.
    – Marj Wyatt
    Dec 14, 2014 at 4:13
  • What happens if you rename the file to home.php? Dec 14, 2014 at 7:07
  • That goes against the latest methods for having a static home page. WordPress is now recommending that file be named front-page.php instead of home.php. When I had my pre_get_posts filter set to only shows, it broke nav_menu_items so it didn't work at all. This site has 2 plugins enabled. I disabled them with no change. The hack I posted above is enough to get me moving forward again on the project but I'd REALLY like to know why my query worked perfectly on archive-shows.php and not front-page.php.
    – Marj Wyatt
    Dec 14, 2014 at 18:10
  • Yes, the is_front_page() conditional won't work with pre_get_posts. You could try index.php codex.wordpress.org/Plugin_API/Action_Reference/… Dec 15, 2014 at 4:44
  • Thanks, Brad. I guess that I solved my own coding problem but now the mystery is solved too.
    – Marj Wyatt
    Dec 15, 2014 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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