0

I made custom post type with one taxonomy. Everything was good except pagination on taxonomy. Firstly, without any changes, I was able to switch pages on main custom post type archive:

website.com/custom_post_type_name/page/x

But when I wanted to jump to taxonomy and switch pages I got 404 error.

website.com/custom_post_type_name/taxonomy_slug/page/x

I tried doing regex from answer from StackExchange but it won't work at all.

add_filter( 'rewrite_rules_array', 'my_insert_rewrite_rules' );
function my_insert_rewrite_rules( $rules ) {
  $newrules = array();
  $newrules['promocje(/[a-z]+)(/page/([0-9]+))?'] =
      'index.php?post_type=promocje'
      . '&shop=$matches[1]&paged=$matches[3]';

  return $newrules + $rules;
}
  • promocje is the name of the post type.
  • shop is the name of the taxonomy.

After that, I was able to switch pages on taxonomy pages, but I had 404 error when paginating the custom post type.

Code of custom post type and taxonomy:

    $args = array(
        'labels' => $labels,
        'hierarchical' => false,
        'description' => 'Promocje filtrowane przez sklep',
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'page-attributes' ),
        'taxonomies' => array( 'shop'),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'menu_icon' => 'dashicons-format-audio',
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'query_var' => true,
        'can_export' => true,
        'capability_type' => 'post',
        'register_meta_box_cb' => 'add_promo_metaboxes',
        'rewrite' => array( 'slug' => 'promocje/%shop%', 'with_front' => false ),
        'has_archive' => 'promocje',
    );
    function wpa_show_permalinks( $post_link, $post ){
    if ( is_object( $post ) && $post->post_type == 'promocje' ){
        $terms = wp_get_object_terms( $post->ID, 'shop' );
        if( $terms ){
            return str_replace( '%shop%' , $terms[0]->slug , $post_link);
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );

And taxonomy:

register_taxonomy(
    'shop',
    'promocje',
    array(
        'hierarchical' => true,
        'label' => 'Sklep',
        'query_var' => true,
        'rewrite' => array(
            'slug' => 'promocje',
            'with_front' => false
        )
    )
);

Any help would be appreciated. I've been trying to solve this for more than a week.

  • It looks like you're missing some code from the post type registration part. – Dave Romsey Oct 5 '16 at 19:08
  • It's registred, I can see them so that's not a problem – kaska3er Oct 5 '16 at 20:26
  • Okay, but I was asking so that your question would be more complete for the people trying to help you :-) – Dave Romsey Oct 5 '16 at 20:30
  • I understand :) But i will try doing Show more ajax loader maybe that would work good – kaska3er Oct 7 '16 at 15:34
  • @DaveRomsey I did this code: global $wp; $current_url = home_url(add_query_arg(array(),$wp->request)); $arrVals = explode("/",$current_url); $found = 0; foreach ($arrVals as $index => $value) { if($value == 'page') $found = $index; } wp_reset_postdata(); global $wp_query; $args = array_merge( $wp_query->query_vars, array( 'post_type' => 'promocje', 'paged' => $arrVals[$place]) ); $pageposts = new WP_Query($args); Now I can get every page using hardcode, I also did a code for getting page number from url. How can i put this in pre_get_posts or parse_query? – kaska3er Oct 7 '16 at 22:19
1

From the Admin Dashboard, Go to Permalink Settings and hit Save Changes. Hope this will solve the problem.

0

I solved it! You need to var_dump wp_query on 1st page and on 404 page

add_action( 'pre_get_posts', 'elo_jlb' );
function elo_jlb($query)
{
    if(!is_admin())
    {
        $query->set('paged', $query->get('page'));
        $query->set('HERE_POST_TYPE_NAME', '');
    }
}

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.