Custom Loop for Custom Post Type

I’m using WP 4.0 and Headway as my theme. I’ve created a custom post type called ‘property’ and am looking to create the loop in 2 columns. but I’m not sure what to add to the following code to make it happen. I’d also like to add pagination.

I also have other info to place inside the loop. just working on getting it working right.

Here’s the code that’s currently working for me… without columns.

<?php $loop = new WP_Query( array( 'post_type' => 'property', 'posts_per_page' => -1, 'category' => 'current' ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="pindex">
    <div class="pimage">
        <a href="https://wordpress.stackexchange.com/questions/165793/<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) {the_post_thumbnail();} ?></a>
    </div>
    <div class="ptitle">
        <h2><?php echo get_the_title(); ?></h2>
    </div>
</div>
<?php endwhile; wp_reset_query(); ?>

1
1

There are couple of issues in your query.

  1. There is not parameter named category. You can use these following.

    cat (int) - use category id.
    category_name (string) - use category slug (NOT name).
    category__and (array) - use category id.
    category__in (array) - use category id.
    category__not_in (array) - use category id.
    
  2. If you need your query to paginate then you should not use posts_per_page' => -1. This will overwrite pagination and return all posts.

  3. One more thing, you are checking for post thumbnail at wrong place. You should check it before the image container.

So I have modified your query and this is how it will be. I am assuming your category slug is current as you used in your query.

<?php
    $loop = new WP_Query( array( 'post_type' => 'property', 'category_name' => 'current', 'ignore_sticky_posts' => 1, 'paged' => $paged ) );
    if ( $loop->have_posts() ) :
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <div class="pindex">
                <?php if ( has_post_thumbnail() ) { ?>
                    <div class="pimage">
                        <a href="https://wordpress.stackexchange.com/questions/165793/<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
                    </div>
                <?php } ?>
                <div class="ptitle">
                    <h2><?php echo get_the_title(); ?></h2>
                </div>
            </div>
        <?php endwhile;
        if (  $loop->max_num_pages > 1 ) : ?>
            <div id="nav-below" class="navigation">
                <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Previous', 'domain' ) ); ?></div>
                <div class="nav-next"><?php previous_posts_link( __( 'Next <span class="meta-nav">&rarr;</span>', 'domain' ) ); ?></div>
            </div>
        <?php endif;
    endif;
    wp_reset_postdata();
?>

This should return all posts in category current with pagination. If you need to get posts from more than one category then you can use category__in parameter instead of category_name.

'category__in' => array( 2, 6 )

Note that category__in accpepts only category IDs.

Leave a Comment