0

I have the exact same issue as Custom Taxonomy archive returns 404, I tried implementing the solution but still finding issues...

here is my CPT

    <?php
    if( ! function_exists( 'videos_create_post_type' ) ) :
        function videos_create_post_type() {
            $labels = array(
                'name' => 'Videos',
                'singular_name' => 'Video',
                'add_new' => 'Add video',
                'all_items' => 'All videos',
                'add_new_item' => 'Add video',
                'edit_item' => 'Edit video',
                'new_item' => 'New video',
                'view_item' => 'View video',
                'search_items' => 'Search videos',
                'not_found' => 'No videos found',
                'not_found_in_trash' => 'No videos found in trash',
                'parent_item_colon' => 'Parent video'
            );
            $args = array(
                'labels' => $labels,
                'public' => true,
                'has_archive' => 'videos',
                'publicly_queryable' => true,
                'query_var' => true,
                'rewrite' => array( 'slug' => 'videos/%videos_category%', 'with_front' => false ),
                'capability_type' => 'post',
                'hierarchical' => false,
                'supports' => array(
                    'title',
                    'editor',
                    'thumbnail',
                    'revisions'
                ),
                'menu_position' => 5,
                'menu_icon'     => 'dashicons-video-alt3',
                'exclude_from_search' => false
            );
            register_post_type( 'videos', $args );
            //flush_rewrite_rules();

            register_taxonomy( 'videos_category', // register custom taxonomy - category
                'videos',
                array(
                    'hierarchical' => true,
                    'public' => true,
                    'show_admin_column' => true,
                    'show_in_nav_menus' => true,
                    'rewrite' => array( 'slug' => 'videos', 'with_front' => false ),
                    'labels' => array(
                        'name' => 'Video categories',
                        'singular_name' => 'Video category',
                    )
                )
            );
        }
        add_action( 'init', 'videos_create_post_type' );

        function wpa_show_permalinks( $post_link, $post ){
            $args = array(
                'orderby'   => 'ID',
                'order'     => 'ASC'
            );
            if ( is_object( $post ) && $post->post_type == 'videos' ){
                $terms = wp_get_object_terms( $post->ID, 'videos_category', $args );
                if( $terms ){
                    return str_replace( '%videos_category%' , $terms[0]->slug , $post_link );
                }
            }
            return $post_link;
        }

    endif; // end of function_exists()
?>

I still get 404's on page/2 my loop props on my archive template is set up like this

                <?php
                    global $post;
                    $custom_taxterms = wp_get_object_terms( $post->ID, 'videos_category', array('fields' => 'ids') );
                    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                    $args = array(
                        'posts_per_page' => 1,
                        'is_paged' => true,
                        'post_type' => 'videos',
                        'paged' => $paged,
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'videos_category',
                                'field' => 'id',
                                'terms' => $custom_taxterms
                            )
                        ),
                    );
                    $custom_query = new WP_Query( $args );
                ?>

And in the while loop i just pass

                @while ($custom_query->have_posts()) @php($custom_query->the_post())
                    @include('partials.content-'.get_post_type())
                @endwhile

I'm using blade templating incase you were wondering about the @while etc.

Help would be much appreciated :) Thanks!

6
  • What does return your loop on page 1 ? If you're on a taxonomy archive page I don't understand why you have to make you're own WP Query.
    – Elex
    Dec 14, 2017 at 17:43
  • Hey man, this returns all posts related to specific categories (my taxonomy videos_categories) and lets me limit the amount of posts without using the standard admin setting. Dec 15, 2017 at 7:10
  • Ok, if you're looking to change the limits of posts_per_page only, you must use pre_get_post hook instead of a new query, tell me if you need the answer, but you can find what you ant here : codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
    – Elex
    Dec 15, 2017 at 7:16
  • Cheers for that, i tried: add_action( 'pre_get_posts', function ( $query ) { if ( is_post_type_archive( 'videos' ) ) { // Display 2 posts for a custom post type called 'videos' $query->set( 'posts_per_page', 2 ); return; } } ); But no luck in changing the amount shown... probably because i'm in a taxonomy page (category) rather than a straight up archive page just showing all video post types? Dec 15, 2017 at 9:33
  • I've tried extending this with add_action( 'pre_get_posts', function ( $query ) { if ( $query->is_tax( 'videos_category' ) && $query->is_main_query() ) { $terms = get_terms( 'videos_category', array( 'fields' => 'ids' ) ); $query->set( 'videos', array( 'post' ) ); $query->set( 'posts_per_page', 2 ); $query->set( 'tax_query', array( 'relation' => 'OR', array( 'taxonomy' => 'videos_category', 'field' => 'id', 'terms' => $terms ) ) ); } return $query;} ); Dec 15, 2017 at 9:49

1 Answer 1

1

So after many trials and errors, I found I had to create a specific rewrite rule to go into my functions.php, one that handles post type 'videos' and the category (taxonomy) 'videos_category':

add_filter('init', function() {
    add_rewrite_rule( 
        '^videos/([^/]*)/([^/]*)/(\d*)?',
        'index.php?videos_category=$matches[1]&p=$matches[2]&paged=$matches[3]',
        'top' 
     ); 
});

I also found this plugin to be really helpful for viewing my rewrite rules: https://wordpress.org/plugins/rewrite-rules-inspector/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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