By default WordPress allows you to categorize your content. This functionality is extremely powerful and allows you to have full control of your content. Using this recipe will allow you to find all posts (custom or pages) that are in the same category. This will allow you to build recommended reading sections, customize your archive pages, or whatever other functionality you can come up when grouped by category.
Step 1: Build the Category Query with WP Query
Add this query to the template you wish to load posts by category:
<?php $args = array( 'post_type' => 'photo_gallery' ); $loop = new WP_Query($args); while ($loop->have_posts()) : $loop->the_post(); ?> <h3><?php echo get_the_title(); ?></h3> <?php endwhile; wp_reset_postdata(); ?>
We used a sample category named “Industries” with a slug of “industries”. You can customize this query in any way you feel necessary using the appropriate arguments for WP Query. The tax_query is where we filter by category.
For Pagination
Use the following to iterate over the results:
<?php $args = array( 'post_type' => 'post_type_name', 'posts_per_page' => 10, 'paged' => (get_query_var('paged') ? get_query_var('paged') : 1) ); $loop = new WP_Query($args); while ($loop->have_posts()) : $loop->the_post(); ?> <h3><?php echo get_the_title(); ?></h3> <?php endwhile; wp_reset_postdata(); ?> <div id="pagination"> <?php $big = 999999999; // need an unlikely integer echo paginate_links(array( 'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))), 'format' => '?paged=%#%', 'prev_text' => __(' Previous'), 'next_text' => __('Next '), 'current' => max(1, get_query_var('paged')), 'total' => $loop->max_num_pages )); ?> </div>