admin管理员组

文章数量:1122846

I made a shortcode to list form entry data that paginates using paginate_links(). I added this shortcode to a regular page and it works fine. If I click on page 2, it refreshes the page and adds the /page/2/ bit to my url: mydomain/slug/page/2/. Good, it works as expected.

Now, I moved this shortcode to a custom post type page (called self-studies) without doing anything to it. The new url is mydomain/self-studies/slug/. But when I go to page 2 it removes the /page/2/ and refreshes the page without it. It should have taken me to mydomain/self-studies/slug/page/2/. By the way, I have tried going directly to the correct link to ensure it was not a broken link, and it does the same thing, redirecting me to the page without the /page/2/. I also tried adding /page/2/ to other pages on the same post type that this shortcode does not exist on, and it does the same thing, so I know it has nothing to do with my paginate_links() code.

I have spent way too many hours trying to figure this out, so I come to you in hopes that someone can point me in the right direction.

Things I've tried:

  1. Changing my permalinks structure
  2. Changing all sorts of values in my custom post type args including 'has_archive' => true, removing the rewrite, etc (see what I have below), and yes I flushed the permalinks afterwards each time
  3. Verified that I don't have any pre_get_posts code that is conflicting
  4. Hooking into redirect_canonical and returning false
  5. Scouring the internet, searching for other questions on StackExchange, and even asking ChatGPT and Gemini (which is usually more counterproductive than helpful btw, lol)

My custom post type:

$args = [
    'label'                 => __( 'Self Studies', 'eri' ),
    'description'           => __( 'Self Studies', 'eri' ),
    'labels'                => $labels,
    'supports'              => [ 'title', 'editor', 'thumbnail', 'author', 'revisions', 'excerpt', 'comments' ],
    'taxonomies'            => [],
    'hierarchical'          => true,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 6,
    'menu_icon'             => 'dashicons-clipboard',
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => false,
    'exclude_from_search'   => true,
    'publicly_queryable'    => true,
    'query_var'             => $this->post_type,
    'capability_type'       => 'page',
    'show_in_rest'          => false,
    'rewrite'               => [ 'slug' => $this->post_type ],
];
register_post_type( $this->post_type, $args );

Any help is greatly appreciated. TIA!

I made a shortcode to list form entry data that paginates using paginate_links(). I added this shortcode to a regular page and it works fine. If I click on page 2, it refreshes the page and adds the /page/2/ bit to my url: mydomain.com/slug/page/2/. Good, it works as expected.

Now, I moved this shortcode to a custom post type page (called self-studies) without doing anything to it. The new url is mydomain.com/self-studies/slug/. But when I go to page 2 it removes the /page/2/ and refreshes the page without it. It should have taken me to mydomain.com/self-studies/slug/page/2/. By the way, I have tried going directly to the correct link to ensure it was not a broken link, and it does the same thing, redirecting me to the page without the /page/2/. I also tried adding /page/2/ to other pages on the same post type that this shortcode does not exist on, and it does the same thing, so I know it has nothing to do with my paginate_links() code.

I have spent way too many hours trying to figure this out, so I come to you in hopes that someone can point me in the right direction.

Things I've tried:

  1. Changing my permalinks structure
  2. Changing all sorts of values in my custom post type args including 'has_archive' => true, removing the rewrite, etc (see what I have below), and yes I flushed the permalinks afterwards each time
  3. Verified that I don't have any pre_get_posts code that is conflicting
  4. Hooking into redirect_canonical and returning false
  5. Scouring the internet, searching for other questions on StackExchange, and even asking ChatGPT and Gemini (which is usually more counterproductive than helpful btw, lol)

My custom post type:

$args = [
    'label'                 => __( 'Self Studies', 'eri' ),
    'description'           => __( 'Self Studies', 'eri' ),
    'labels'                => $labels,
    'supports'              => [ 'title', 'editor', 'thumbnail', 'author', 'revisions', 'excerpt', 'comments' ],
    'taxonomies'            => [],
    'hierarchical'          => true,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 6,
    'menu_icon'             => 'dashicons-clipboard',
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => false,
    'exclude_from_search'   => true,
    'publicly_queryable'    => true,
    'query_var'             => $this->post_type,
    'capability_type'       => 'page',
    'show_in_rest'          => false,
    'rewrite'               => [ 'slug' => $this->post_type ],
];
register_post_type( $this->post_type, $args );

Any help is greatly appreciated. TIA!

Share Improve this question edited Jul 19, 2024 at 21:51 Aristocles asked Jul 19, 2024 at 21:28 AristoclesAristocles 1397 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Are you making changes to the query in the template at all? If you modify the main query you often have to include "Paged" parameter, as explained here: https://codex.wordpress.org/Pagination

If WP_Query is altering the main loop and the "paged" parameter is not set you'll need to add it with get_query_var(). This is so WordPress knows exactly what page it's on.

It's hard to know without seeing the code for the actual template you're using, but that's a potential path to explore.

本文标签: phpCustom post type removing page2 from URL when trying to access it