• I create own Custom Post Type and add to functions.php

    add_action( 'init', 'create_artists' );
    function create_artists() {
    	register_post_type( 'artistspage',
    		array(
    			'labels' => array(
    				'name'                => _x( 'Artists', 'Post Type General Name', 'text_domain' ),
                    'singular_name'       => _x( 'Artist', 'Post Type Singular Name', 'text_domain' ),
                    'menu_name'           => __( 'Artist', 'text_domain' ),
                    'parent_item_colon'   => __( 'Parent Artist:', 'text_domain' ),
                    'all_items'           => __( 'All Artists', 'text_domain' ),
                    'view_item'           => __( 'View Artist', 'text_domain' ),
                    'add_new_item'        => __( 'Add New Artist', 'text_domain' ),
                    'add_new'             => __( 'New Artist', 'text_domain' ),
                    'edit_item'           => __( 'Edit Artist', 'text_domain' ),
                    'update_item'         => __( 'Update Artist', 'text_domain' ),
                    'search_items'        => __( 'Search Artists', 'text_domain' ),
                    'not_found'           => __( 'No products found', 'text_domain' ),
                    'not_found_in_trash'  => __( 'No products found in Trash', 'text_domain' ),
    			),
    			'public'              => true,
    			'rewrite'             => array('slug' => 'artists', 'with_front' => false),
                'supports'            => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'custom-fields'),
                'hierarchical'        => false,
                'public'              => true,
                'show_ui'             => true,
                'show_in_menu'        => true,
                'show_in_nav_menus'   => true,
                'show_in_admin_bar'   => true,
                'menu_position'       => 5,
                'can_export'          => true,
                'has_archive'         => true,
                'exclude_from_search' => false,
                'publicly_queryable'  => true,
                'capability_type'     => 'page',
                'has_archive' => true
    		)
    	);
        //flush_rewrite_rules();
    }

    Then I add 7 Custom Post data on it and try to call them at archive-artistspage.php by following this code

    <?php  
    
                            if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
                            elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
                            else { $paged = 1; }
                            $args = array (
                                'post_type'          => 'artistspage',
                                'posts_per_page'     => 6,
                                'order'              => 'ASC',
                                'orderby'            => 'modified',
                                'paged'              => $paged
                            );
                            // The Query
                            $wp_query = new WP_Query( $args );
                            //print_r($wp_query->request);
                            while ( $wp_query->have_posts() ) : $wp_query->the_post();
                            ?>
                                <div class="thumb-who">
                                    <a href="<?php the_permalink(); ?>">
                                    <?php the_post_thumbnail('who-thumb'); ?>
                                    </a>
                                    <h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
                                    <p><?php the_excerpt(); ?></p>
                                    <a href="<?php the_permalink() ?>" id="read-more" rel="bookmark">Read More ></a>
                                </div>
                            <?php
    
                            endwhile;
                            wp_pagenavi( array( 'query' => $wp_query ) );
                            wp_reset_postdata();	// avoid errors further down the page
    
                        ?>

    I get result at https://www.domain.tld/artists but get 404 error when I am click on Pagenavi 2

    But I am get the Page 2 result at https://domain.tld/?post_type=artistspage&page=2

    I try to add this on my functions.php

    add_action('init', 'event_archive_rewrite');
    function event_archive_rewrite(){
        add_rewrite_rule('^artists/([^/]*)/([^/]*)/?','index.php?post_type=artistspage&page=$matches[2]','top');
    }

    but no success.

    Where I am doing wrong?

    thank you

  • The topic ‘Custom Post Type Error’ is closed to new replies.