1

I started noticing issues last week when 4.5 was released and mistakenly believed that update caused an issue. In reality, I just happened to notice it for the first time around then because last week was the first time a set of conditions was created. With this knowledge, I was wondering if anyone could possibly help me figure out what I'm missing as I try to tackle my issue.

For background, I use a PHP script to display gallery thumbnails fron a Coppermine gallery, with pagination handled utilizing parameter mdgpage for any pages over the initial one (similar to the built-in page parameter). I utilize this on pages of varying URL depths and have had no issues since getting it working. For better organization, we've been starting to create our own custom post types. This has been working fine, however last week was the first time a gallery in a custom post type entry justified more than one page, and subsequent gallery pages 404.

Where I get confused is why pages and custom post types (and standard posts though I don't plan to use galleries in those) behave differently in this sense. Checking rewrite rules and regex implies that there's no difference there. Here is what I have set for my query_vars and rewrite_rules:

function add_query_vars($aVars) {
$aVars[] = "mdgpage"; // represents the name of the product category as shown in the URL
return $aVars;
}

// hook add_query_vars function into query_vars
add_filter('query_vars', 'add_query_vars');

function add_rewrite_rules($aRules) {
$aNewRules = array('(.?.+?)/mdgpage/([^/]+)/?$' => 'index.php?pagename=$matches[1]&mdgpage=$matches[2]');
$aRules = $aNewRules + $aRules;
return $aRules;
}

// hook add_rewrite_rules function into rewrite_rules_array
add_filter('rewrite_rules_array', 'add_rewrite_rules');

Also here are the CPT declarations that we made:

function md_custom_doujinshi_en() {
    $labels = array(
        'name'               => _x( 'Doujinshi (en)', 'post type general name' ),
        'singular_name'      => _x( 'Doujinshi (en)', 'post type singular name' ),
        'add_new'            => _x( 'Add Doujinshi (en)', 'Doujinshi (en)' ),
        'add_new_item'       => __( 'Add Doujinshi (en)' ),
        'edit_item'          => __( 'Edit Doujinshi' ),
        'new_item'           => __( 'New Doujinshi' ),
        'all_items'          => __( 'All Doujinshi (en)' ),
        'view_item'          => __( 'View Doujinshi' ),
        'search_items'       => __( 'Search Doujinshi' ),
        'not_found'          => __( 'No Doujinshi found' ),
        'not_found_in_trash' => __( 'No Doujinshi found in the Trash' ),
        'parent_item_colon'  => '',
        'menu_name'          => 'Doujinshi'
    );
    $args = array(
        'labels'                => $labels,
        'description'           => 'Miss Dream Doujinshi (en)',
        'public'                => true,
        'menu_position'         => 5,
        'supports'              => array( 'title', 'editor', 'thumbnail', 'comments' ),
        'has_archive'           => false,
        'register_meta_box_cb'  => 'md_doujinshi_metaboxes',
        'rewrite'               => array('slug' => 'sailor-moon-doujinshi')
    );
    register_post_type('doujinshi_en', $args); 
}
add_action('init', 'md_custom_doujinshi_en');

function md_custom_doujinshi_jp() {
  $labels = array(
    'name'               => _x( 'Doujinshi (jp)', 'post type general name' ),
    'singular_name'      => _x( 'Doujinshi (jp)', 'post type singular name' ),
    'add_new'            => _x( 'Add Doujinshi (jp)', 'Doujinshi (jp)' ),
    'add_new_item'       => __( 'Add Doujinshi' ),
    'edit_item'          => __( 'Edit Doujinshi' ),
    'new_item'           => __( 'New Doujinshi' ),
    'all_items'          => __( 'All Doujinshi (jp)' ),
    'view_item'          => __( 'View Doujinshi' ),
    'search_items'       => __( 'Search Doujinshi' ),
    'not_found'          => __( 'No Doujinshi found' ),
    'not_found_in_trash' => __( 'No Doujinshi found in the Trash' ),
    'parent_item_colon'  => ''
  );
  $args = array(
    'labels'            => $labels,
    'description'       => 'Miss Dream Doujinshi (jp)',
    'public'            => true,
    'show_ui'           => true,
    'show_in_menu'      => 'edit.php?post_type=doujinshi_en',
    'supports'          => array( 'title', 'editor', 'thumbnail', 'comments' ),
    'has_archive'       => false,
    'register_meta_box_cb'  => 'md_doujinshi_metaboxes',
    'rewrite'         => array('slug' => 'raw-sailor-moon-doujinshi')
  );
  register_post_type('doujinshi_jp', $args);  
}
add_action('init', 'md_custom_doujinshi_jp');

In both pages and the custom post types, the gallery script is set to run and display in the middle of the content. I use a plugin to be able to set the aid and include the script within pages. For the custom post type, the same lines are coded into the page templates where the aid is set through one of the postmeta fields.

Any help or advice to get me in the right direction would be greatly appreciated. Thank you!

1

Your rule doesn't work for custom post types because pagename is the query var specific to the page post type. Your CPT query vars are the slugs you registered them with, doujinshi_en and doujinshi_jp.

You might be able to fix it by adding in additional post types via pre_get_posts:

function wpd_add_custom_types( $query ){
    if( $query->is_main_query() && isset( $query->query_vars['mdgpage'] ) ){
        $query->set( 'post_type', array( 'page', 'doujinshi_en', 'doujinshi_jp' ) );
    }
}
add_action( 'pre_get_posts', 'wpd_add_custom_types' );

But this will break if different post types share the same slug.

However, for your purposes you should have a look at using add_rewrite_endpoint instead, which takes care of generating the proper rules for you.

function wpd_add_endpoint(){
    add_rewrite_endpoint( 'mdgpage', EP_PERMALINK | EP_PAGES );
}
add_action( 'init', 'wpd_add_endpoint' );
  • add_rewrite_endpoint is the perfect solution, thank you very much! I removed the previous rewrite rules, added in the code you provided, and both pages and this custom post types are working perfectly. Once again, many thanks! =) – MarioKnight Apr 23 '16 at 22:37

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.