• Resolved Cactushead

    (@cactushead)


    I created a custom post type and can view individual posts just fine. I can click on the previous and next links to scroll through all the custom post pages.

    When I go to the archive page that I created, I can see several posts as expected – and the pagination shows I’m on page 1 and there are other pages available.

    If I hover over one of the page links I get a link like https://www.mysite.com/video/page/2/
    clicking on any page links goes to the 404 page

    I searched for a long time and most people seem to have the same code.

    I have the following at the top of my archive page

    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    	$args = array( 
    			'post_type' 	 => 'video',			
    			'posts_per_page' => 2,			
    			'paged'          => $paged,
    	);	
    	$query = new WP_Query( $args );

    and lower down I have

    echo paginate_links( array(
    	'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
    	'total'        => $query->max_num_pages,
    	'current'      => max( 1, get_query_var('paged') ),
    	'format'       => '/page/%#%', //was '?paged=%#%'
    	'show_all'     => false,
    	'type'         => 'plain',
    	'end_size'     => 2,
    	'mid_size'     => 1,
    	'prev_next'    => true,
    	'prev_text'    => sprintf( '<i></i> %1$s', __( '? Previous', 'text-domain' ) ),
    	'next_text'    => sprintf( '%1$s <i></i>', __( 'Next ?', 'text-domain' ) ),
    	'add_args'     => false,
    	'add_fragment' => '',
    	'before_page_number' => '',
    	'after_page_number'  => ''
    ) );

    Any idea what I’m missing? your help would be appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    My first question is if you have “has archive” set to true in your post type settings so that WP could handle the topic. Then you could just go to https://www.mysite.com/video and it just works.

    Otherwise, it’s a case, as you have above, of doing a custom WP_Query call and trying to get pagination working with that. I would recommend checking out https://docs.pluginize.com/article/81-cleanly-done-pagination-with-custom-wpquery-objects which I typed up a couple years back and whatnot. May provide an alternative thing to try out for you.

    Thread Starter Cactushead

    (@cactushead)

    That is what I thought I should do – I have “has archive” set to true

    function cptui_register_my_cpts_video() {
    
    	/**
    	 * Post Type: Videos.
    	 */
    
    	$labels = array(
    		"name" => __( "Videos", "custom-post-type-ui" ),
    		"singular_name" => __( "Video", "custom-post-type-ui" ),
    	);
    
    	$args = array(
    		"label" => __( "Videos", "custom-post-type-ui" ),
    		"labels" => $labels,
    		"description" => "",
    		"public" => true,
    		"publicly_queryable" => true,
    		"show_ui" => true,
    		"delete_with_user" => false,
    		"show_in_rest" => true,
    		"rest_base" => "",
    		"rest_controller_class" => "WP_REST_Posts_Controller",
    		"has_archive" => true,
    		"show_in_menu" => true,
    		"show_in_nav_menus" => true,
    		"exclude_from_search" => false,
    		"capability_type" => "post",
    		"map_meta_cap" => true,
    		"hierarchical" => false,
    		"rewrite" => array( "slug" => "video", "with_front" => true ),
    		"query_var" => true,
    		"menu_position" => 5,
    		"menu_icon" => "dashicons-video-alt3",
    		"supports" => array( "title", "editor", "thumbnail", "excerpt", "revisions", "author" ),
    		"taxonomies" => array( "category", "post_tag" ),
    	);
    
    	register_post_type( "video", $args );
    }
    
    add_action( 'init', 'cptui_register_my_cpts_video' );
    

    Thanks for the link I’ll check it out

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Assuming your active theme has a decent archive.php that has pagination links already, you should be good for your video archive url and getting pagination there.

    Thread Starter Cactushead

    (@cactushead)

    OK, after literally days trying everything – for some reason, it now works using the default pagination.
    I commented out the custom pagination (as I tried before) and suddenly it works this time – quite bizarre.
    Thanks for your help and suggestions it was much appreciated.

    UPDATE – now I tried my custom pagination again and it now works too!!!!!!
    No idea what is happening or why it did not work before but hey, I’ll take it.

    • This reply was modified 5 years, 1 month ago by Cactushead.
    • This reply was modified 5 years, 1 month ago by Cactushead.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Yay! working pagination one way or another!

    Let us know if you need anything else.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘archive pagination does not work’ is closed to new replies.