0

I am working on a little Plugin (based on a Custom Post Type called gallery) for a gallery (that reads out a large number of ftp folders). The post type does not have taxonomies but like pages just parent pages with many child pages inside.

Some of these child pages are year numbers, example for the path:

gallery/appearances/2023/2023-11-05/

I managed to allow the slug 2023 with the help of the code of the very old plugin by viper007bond "Allow Numeric Slugs", just with changing the code so it works for the custom post type gallery only and after my search here, I switched to this (newer) solution: How to add numeric slug for child page in WordPress 5.9?

My problem is now, that everything works EXCEPT the gallery pages that are only numeric, like gallery/appearances/2023/ - this redirects to gallery/appearances/

This is a known, yet unsolved problem of the "Allow Numeric Slugs" plugin and it was also not finally resolved in the above topic.

I don't want to resolve this side wide code (if you can, the better, because there are many people who looked for a solution of this), but just for the Custom Post Type "gallery": Here's the original code:

function page_rewrite_rules( $rules ) {
        unset( $rules['(.?.+?)(/[0-9]+)?/?$'] );     // Before WordPress 4.4
        unset( $rules['(.?.+?)(?:/([0-9]+))?/?$'] ); // After WordPress 4.4, see https://core.trac.wordpress.org/changeset/34492

        $rules['(.?.+?)?/?$'] = 'index.php?pagename=$matches[1]';

        return $rules;
    }

OR for the newer code from How to add numeric slug for child page in WordPress 5.9?

add_action( 'init', 'my_add_page_rewrite_rules' );
function my_add_page_rewrite_rules() {
    add_rewrite_rule(
        '^(.+/\d{4})(?:/(\d+))?/?$',
        'index.php?pagename=$matches[1]&page=$matches[2]',
        'top' );
}

or

add_filter( 'page_rewrite_rules', 'my_page_rewrite_rules' );
function my_page_rewrite_rules( $page_rewrite ) {
    return array(
        '^(.+/\d{4})(?:/(\d+))?/?$' => 'index.php?pagename=$matches[1]&page=$matches[2]',
    ) + $page_rewrite;
}

Here's my try (one of many)

function page_rewrite_rules( $rules ) {

    $rules['gallery/(.*)?'] = 'index.php?post_type=gallery&p=$matches[1]';

    return $rules;
}

Unfortunately, both does not work.

Note: the 2023 may be in another position or length or there may be multiple number only slugs.

Can you help me, please? Thanks, Martina.

2 Answers 2

0

You need to write a rewrite URL for the galley, please see the below code and paste into your active theme functions.php file

function gallery_custom_rewrite_rule() {
    add_rewrite_tag('%gallery_id%', '([^&]+)');
    
    // Rule to handle numeric slugs in a gallery post type
    add_rewrite_rule(
        '^gallery/(.+)/([0-9]+)/?$',
        'index.php?post_type=gallery&gallery_id=$matches[2]',
        'top'
    );
}

add_action('init', 'gallery_custom_rewrite_rule', 10, 0);

function gallery_query_vars($query_vars) {
    $query_vars[] = 'gallery_id';
    return $query_vars;
}

add_filter('query_vars', 'gallery_query_vars');
1
  • Thanks, Jenny. That's where I was stuck. I tried so many possibilities of rewrite codes and none worked. Unfortunately, yours also does not work. I can now call gallery/appearances/2023/ (which is better than having it redirected to gallery/appearances) but it displays the exact content of gallery/ now. So, it's better, but not the solution. Commented Jan 5 at 11:45
0

I edited Jenny's code a little bit and could find the right rewrite rule thanks to her. Just this rewrite rule is needed, not the other parts (rewrite tag and query vars) to make it work.

function gallery_custom_rewrite_rule() {  
    // Rule to handle numeric slugs in a gallery post type
    add_rewrite_rule(
        '^gallery/(.+)/([0-9]+)/?$',
        'index.php?post_type=gallery&gallery=$matches[1]/$matches[2]',
        'top'
    );
}
add_action('init', 'gallery_custom_rewrite_rule', 10, 0);

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.