• Resolved Marj Wyatt

    (@marjwyatt)


    I’m baffled and truly hope someone will help.

    My client has requisitioned a front page layout that has both widgets and two lists, one with most recent posts and the other with custom post types that are set up and working properly.

    My front-page template does mostly what I need it to do. The snag that I’ve hit is with isolating the custom post types. Here is my query:

    echo '<aside class="home-bottom-right widget-area two-thirds last">';
    wp_reset_query();
    $args = array( 'post_type' => 'shows', 'posts_per_page' => 10 );
    $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;
    echo '</aside>';

    The echo of $postID, $postDate, and $postType is for troubleshooting purposes. The query is pulling all post types, including nav items, pages, posts and, of course, my custom post type.

    I’ve been stuck on this chunk of code for 2 days now and I’m quite frustrated. I’m starting to wonder if it is possible to do what my client wants, although I do hate to cry uncle.

    As I said in the opening statement, I truly hope that someone out there in WordPress land will have some guidance for me.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Marj Wyatt

    (@marjwyatt)

    I just put the same query onto archive-shows.php and it is properly isolating the custom post types only.

    Is there some different code that I should be using to do this on a front-page.php template?

    Anyone?

    Thread Starter Marj Wyatt

    (@marjwyatt)

    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’.

    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.

    Thread Starter Marj Wyatt

    (@marjwyatt)

    Okay, I got the problem solved but the solution creates a new question. Here is what made it work.

    This code was added to front-page.php:

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

    And this is the new query:

    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>';

    The part of the query that provokes a new question is this:

    Why, if the query was set up with one arg, specifically post_type=>’shows’, did I have to redundantly ensure that I was dealing with a ‘shows’ post to make it work?

    Thread Starter Marj Wyatt

    (@marjwyatt)

    I guess no one had any ideas. I’ll mark this resolved.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘query for custom post types isn't working’ is closed to new replies.