php - Multiple WP_Query With Custom Post Type & Pagination -
basically have 2 post types set up, "dog" , "cat". have page template 2 wp_query loops displaying 2 latest posts each post type in each loop. below how i'm registering post types:
<?php function cat_post_type() { register_post_type( 'cat', array('labels' => array( 'name' => __('cats', 'bonestheme'), 'singular_name' => __('cat', 'bonestheme'), 'all_items' => __('all cats', 'bonestheme'), 'add_new' => __('add new', 'bonestheme'), 'add_new_item' => __('add new cat', 'bonestheme'), 'edit' => __( 'edit', 'bonestheme' ), 'edit_item' => __('edit cats', 'bonestheme'), 'new_item' => __('new cat', 'bonestheme'), 'view_item' => __('view cat', 'bonestheme'), 'search_items' => __('search cat', 'bonestheme'), 'not_found' => __('nothing found in database.', 'bonestheme'), 'not_found_in_trash' => __('nothing found in trash', 'bonestheme'), 'parent_item_colon' => '' ), 'description' => __( 'this cat post type', 'bonestheme' ), 'public' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'show_ui' => true, 'query_var' => true, 'menu_position' => 8, 'rewrite' => array( 'slug' => 'cat', 'with_front' => false ), 'has_archive' => 'cats', 'capability_type' => 'post', 'hierarchical' => false, 'supports' => array( 'title', 'editor', 'thumbnail' ) ) ); } add_action( 'init', 'cat_post_type' ); function dog_post_type() { register_post_type( 'dog', array('labels' => array( 'name' => __('dogs', 'bonestheme'), 'singular_name' => __('dog', 'bonestheme'), 'all_items' => __('all dogs', 'bonestheme'), 'add_new' => __('add new', 'bonestheme'), 'add_new_item' => __('add new dog', 'bonestheme'), 'edit' => __( 'edit', 'bonestheme' ), 'edit_item' => __('edit dogs', 'bonestheme'), 'new_item' => __('new dog', 'bonestheme'), 'view_item' => __('view dog', 'bonestheme'), 'search_items' => __('search dog', 'bonestheme'), 'not_found' => __('nothing found in database.', 'bonestheme'), 'not_found_in_trash' => __('nothing found in trash', 'bonestheme'), 'parent_item_colon' => '' ), 'description' => __( 'this dog post type', 'bonestheme' ), 'public' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'show_ui' => true, 'query_var' => true, 'menu_position' => 8, 'rewrite' => array( 'slug' => 'dog', 'with_front' => false ), 'has_archive' => 'dogs', 'capability_type' => 'post', 'hierarchical' => false, 'supports' => array( 'title', 'editor', 'thumbnail' ) ) ); } add_action( 'init', 'dog_post_type' ); ?>
and below how displaying post types:
<?php $cat_query = new wp_query( array( 'posts_per_page' => 2, 'post_type' => 'cat' ) ); if($cat_query->have_posts()) : ?> <div> <?php while($cat_query->have_posts()) : $cat_query->the_post(); ?> <div> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </div> <?php endwhile; wp_reset_postdata(); ?> </div> <?php endif; ?> <?php $dog_query = new wp_query( array( 'posts_per_page' => 2, 'post_type' => 'dog' ) ); if($dog_query->have_posts()) : ?> <div> <?php while($dog_query->have_posts()) : $dog_query->the_post(); ?> <div> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </div> <?php endwhile; wp_reset_postdata(); ?> </div> <?php endif; ?>
i want them able co-exist own pagination, here (watch video) seems work normal wp posts, not custom post types. if has idea on how achieve this, amazing because driving me crazy!
edit
i've achieved following code it's buggy, example, if go page 3 on both paginations, click page 1 on cat post type pagination, go same url it's on:
<?php $paged1 = isset( $_get['paged1'] ) ? (int) $_get['paged1'] : 1; $cat_query = new wp_query( array( 'posts_per_page' => 2, 'post_type' => 'cat', 'paged' => $paged1 ) ); if($cat_query->have_posts()) : ?> <div> <?php while($cat_query->have_posts()) : $cat_query->the_post(); ?> <div> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </div> <?php endwhile; echo paginate_links( array( 'format' => '?paged1=%#%', 'current' => $paged1, 'total' => $cat_query->max_num_pages, 'add_args' => array( 'paged2' => $paged2 ) ) ); wp_reset_postdata(); ?> </div> <?php endif; ?> <?php $paged2 = isset( $_get['paged2'] ) ? (int) $_get['paged2'] : 1; $dog_query = new wp_query( array( 'posts_per_page' => 2, 'post_type' => 'dog', 'paged' => $paged2 ) ); if($dog_query->have_posts()) : ?> <div> <?php while($dog_query->have_posts()) : $dog_query->the_post(); ?> <div> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </div> <?php endwhile; echo paginate_links( array( 'format' => '?paged2=%#%', 'current' => $paged2, 'total' => $dog_query->max_num_pages, 'add_args' => array( 'paged1' => $paged1 ) ) ); wp_reset_postdata(); ?> </div> <?php endif; ?>
so here how can implement pagination custom post types
<?php $paged1 = isset( $_get['paged1'] ) ? (int) $_get['paged1'] : 1; $cat_query = new wp_query( array( 'paged' => $paged1, 'posts_per_page' => 2, 'post_type' => 'cat' ) ); if($cat_query->have_posts()) : ?> <div> <?php while($cat_query->have_posts()) : $cat_query->the_post(); ?> <div> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </div> <?php endwhile; ?> </div> <?php endif; $pag_args1 = array( 'format' => '?paged1=%#%', 'current' => $paged1, 'total' => $cat_query->max_num_pages, 'add_args' => array( 'paged1' => $paged1 ) ); echo paginate_links( $pag_args1 ); wp_reset_postdata(); wp_reset_query(); ?> <?php $cat_query1 = new wp_query( array( 'paged' => $paged1, 'posts_per_page' => 2, 'post_type' => 'dog' ) ); if($cat_query1->have_posts()) : ?> <div> <?php while($cat_query1->have_posts()) : $cat_query1->the_post(); ?> <div> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </div> <?php endwhile; ?> </div> <?php endif; $pag_args2 = array( 'format' => '?paged1=%#%', 'current' => $paged1, 'total' => $cat_query1->max_num_pages, 'add_args' => array( 'paged1' => $paged1 ) ); echo paginate_links( $pag_args2 ); wp_reset_postdata(); wp_reset_query(); ?>
hope works fine
edit
<?php $paged1 = isset( $_get['paged1'] ) ? (int) $_get['paged1'] : 1; $cat_query = new wp_query( array( 'posts_per_page' => 2, 'post_type' => 'cat', 'paged' => $paged1 ) ); if($cat_query->have_posts()) : ?> <div> <?php while($cat_query->have_posts()) : $cat_query->the_post(); ?> <div> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </div> <?php endwhile; echo paginate_links( array( 'format' => '?paged1=%#%', 'current' => $paged1, 'total' => $cat_query->max_num_pages, 'add_args' => array( 'paged1' => $paged1 ) ) ); wp_reset_postdata(); ?> </div> <?php endif; ?> <?php $paged2 = isset( $_get['paged2'] ) ? (int) $_get['paged2'] : 1; $dog_query = new wp_query( array( 'posts_per_page' => 2, 'post_type' => 'dog', 'paged' => $paged2 ) ); if($dog_query->have_posts()) : ?> <div> <?php while($dog_query->have_posts()) : $dog_query->the_post(); ?> <div> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </div> <?php endwhile; echo paginate_links( array( 'format' => '?paged2=%#%', 'current' => $paged2, 'total' => $dog_query->max_num_pages, 'add_args' => array( 'paged2' => $paged2 ) ) ); wp_reset_postdata(); ?> </div> <?php endif; ?>
Comments
Post a Comment