1

I have several custom post types (created through CPT UI plugin). I want to display posts from these post types on my page, for example "News". I've created a page and inserted in it shortcode [show_posts post_type="novyny"]. I've got a plugin that processes this shortcode

function foo($args){
    $post_type = $args['post_type'];
    $custom_post_type = new WP_Query(
        array(
            'post_type' => $post_type,
            'orderby' => 'date',
            'order' => 'DESC',
            'posts_per_page' => 4
        )
    );

    if( $custom_post_type->have_posts() ) :
        while( $custom_post_type->have_posts() ): $custom_post_type->the_post(); ?>
            <h1><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h1>
            <p> <?php the_modified_date(); echo ", "; the_modified_time() ?></p>
            <p> <?php the_excerpt() ?></p>
        <?php endwhile;
        the_posts_pagination();
    endif;
    wp_reset_postdata();
}

add_shortcode('show_posts', 'foo');

Posts are displayed, but pagination blog doesn't appear at the page even if there is more than 4 posts to display. What's wrong?

  • pagination functions operate on the main query, you have a custom query. you're also not passing any pagination parameters to the query in the shortcode, so even if you were to visit other pages, you'd get the same results. I suggest searching around here for paginating custom queries, there are no shortage of questions and answers on the subject. – Milo Feb 5 '16 at 17:04
3

the_posts_pagination uses global query and global query does not have pagination. So it is the correct behavior of WordPress.

To overcome from this problem assign custom query to global query then after looping again restore the global query.

function foo($args){
    $post_type = $args['post_type'];
    global $wp_query;
    $original_query = $wp_query;
    $custom_post_type = new WP_Query(
        array(
            'post_type' => $post_type,
            'orderby' => 'date',
            'order' => 'DESC',
            'posts_per_page' => 4
        )
    );

    $wp_query = $custom_post_type;

    if( $custom_post_type->have_posts() ) :
        while( $custom_post_type->have_posts() ): $custom_post_type->the_post(); ?>
            <h1><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h1>
            <p> <?php the_modified_date(); echo ", "; the_modified_time() ?></p>
            <p> <?php the_excerpt() ?></p>
        <?php endwhile;
        the_posts_pagination();
    endif;
    wp_reset_postdata();

    $wp_query = $original_query;
}
2

Thank you all, guys. I've solved it this way:

  • added $paged variable: $paged = ( get_query_var('page') ) ? get_query_var('page') : 1; (for main page you should use get_query_var('page'), for other pages - $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; )
  • And added a new argument for WP_Query: 'paged' => $paged

My code now:

function foo($args){
    $post_type = $args['post_type'];

    /* Added $paged variable */
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

    global $wp_query;
    $original_query = $wp_query;

    $query_args = array(
        'post_type' => $post_type,
        'posts_per_page' => 3,

        /* And added a new argument for WP_Query */
        'paged' => $paged
    );

    $custom_post_type = new WP_Query( $query_args );
    $wp_query = $custom_post_type;

    if ( $custom_post_type->have_posts() ) :
        while ( $custom_post_type->have_posts() ) :
            $custom_post_type->the_post(); ?>
                <h1><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h1>
                <p> <?php the_modified_date(); echo ", "; the_modified_time() ?></p>
                <p> <?php the_excerpt() ?></p>
        <?php endwhile;
        the_posts_pagination();
    endif;

    wp_reset_postdata();
    $wp_query = $original_query;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.