Search code examples
wordpressjquery-isotopeposts

Wordpress query to include posts AND pages


I am implementing the isotope script within a wordpress site, and have the following query currently to populate the grid. I would like Wordpress pages to be treated the same as posts, can this be achieved?

Thank you in advance for any assistance.

<?php $the_query = new WP_Query( 'posts_per_page=10' ); //Check the WP_Query docs to see how you can limit which posts to display ?>

<?php if ( $the_query->have_posts() ) : ?>
<div id="isotope-grid">
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); 
    $termsArray = get_the_terms( $post->ID, "category" );  //Get the terms for this particular item
    $termsString = ""; //initialize the string that will contain the terms
    foreach ( $termsArray as $term ) { // for each term 
    $termsString .= $term->slug.' '; //create a string that has all the slugs 
    }
    ?> 

<article class="<?php echo $termsString; ?> isotope-brick">

        <?php if ( has_post_thumbnail() ) { 
                  the_post_thumbnail();
            } ?>
</article><!-- end article -->

<?php endwhile;  ?>
</div> <!-- end isotope-grid -->

Solution

  • Your query will shows posts by default. Add the 'post_type' parameter to include pages as well.

    Change:

    <?php $the_query = new WP_Query( 'posts_per_page=10' ); //Check the WP_Query docs to see how you can limit which posts to display ?>
    

    To:

    <?php $the_query = new WP_Query( array(
        'post_type'      => array( 'post', 'page', ),
        'posts_per_page' => 10,
    ) ); ?>
    

    Further reading: http://codex.wordpress.org/Class_Reference/WP_Query