0

I was trying to use a custom URL to load a page template for single-events.php without rewriting the default URL format. I am using filter template_include to load the template like this:

add_filter('template_include','include_template_for_single_event',99);
function include_template_for_single_event($template)
{
    //check_for_single_event() will check if the current url = custom url
    if( check_for_single_event() ){
        $new_template = locate_template(array('single-events.php','single.php'));
        if($new_template)$template = $new_template;
    }     
   return $template;
}

The issue with this is that the contents of the single cpt within the loop is not loading which means there is no data for the single cpt such as get_the_ID(), have_posts() etc

Edit:

I do have a default link for a single post of a custom post type

example.com/nepal/events/visit-year-2020 

Now, I want to load this same page template with URL

example.com/nepal/events/visit-year-2020/2020-01-10. 

P.S. client still want the default URL and this new URL with every date available appended to the end.

2
  • Can you elaborate on what you're trying to achieve? Commented Jan 10, 2020 at 12:27
  • @JacobPeattie I do have a default link for a single post of a custom post type example.com/nepal/events/visit-year-2020 Now, I want to load this same page template with URL example.com/nepal/events/visit-year-2020/2020-01-10. P.S. client still want the default URL and this new URL with every date available appended to the end.
    – dbp
    Commented Jan 10, 2020 at 12:32

1 Answer 1

0

I don't know what nepal is (text prefix only or maybe taxonomy term), but it is not the most important thing, you will correct it if necessary. Rewrite rule like this should resolve the case.

add_action( 'init', 'se356109_events_custom_rule' );
function se356109_events_custom_rule()
{
    add_rewrite_rule(
        'nepal/events/(.+?)/([0-9\-]+)(:?/page/?([0-9]+))?/?$',
        'index.php?events=$matches[1]&paged=$matches[3]',
        'top'
    );
}

If you need to use URL date (in your example "2020-01-10") somewhere, you need to add a new query variable (with any name, e.g. custom_dt) and modify rewrite rule.

add_action( 'init',       'se356109_events_custom_rule' );
add_filter( 'query_vars', 'se356109_query_vars' );

function se356109_query_vars( $vars )
{
    $vars[] = 'custom_dt';
    return $vars;
}

function se356109_events_custom_rule()
{
    add_rewrite_rule(
        'nepal/events/(.+?)/([0-9\-]+)(:?/page/?([0-9]+))?/?$',
        'index.php?events=$matches[1]&paged=$matches[3]&custom_dt=$matches[2]',
        'top'
    );
}

And that's how you read the value of this new variable:

$dt = get_query_var('custom_dt', false);
11
  • I am on localhost right now and this is not working. maybe coz the url is localhost/tour/nepal/events/.. rather than example.com/nepal/events.. ?
    – dbp
    Commented Jan 12, 2020 at 5:02
  • Permalinks will need to be refreshed (you can do this from your admin under Settings > Permalinks -> Save) before the rewrite changes will take effect. I forgot to mention it.
    – nmr
    Commented Jan 12, 2020 at 16:46
  • Still not working. My localhost link to the page is: http://localhost/tour/nepal/events/visit-year-2020/2020-01-13. Both ('nepal/events/(.+?)/([0-9\-]+) and ('tour/nepal/events/(.+?)/([0-9\-]+) is not working.
    – dbp
    Commented Jan 13, 2020 at 4:00
  • I am using just 'tour/nepal/events/(.+?)/([0-9\-]+)(:?/page/?([0-9]+))?/?$'. No luck
    – dbp
    Commented Jan 13, 2020 at 11:38
  • events is custom post type, and what is tour and nepal? Can you copy the results of this code and paste them, e.g. in https://pastebin.com/? add_action( 'wp_footer', 'se_footer_rewrite_dump', 80 ); function se_footer_rewrite_dump() { global $wp_rewrite; echo '<pre>RULES-TOP <br>'.print_r($wp_rewrite->extra_rules_top, true); echo '<br><br>PERMASTRUCTS<br>'.print_r($wp_rewrite->extra_permastructs, true).'</pre>'; }
    – nmr
    Commented Jan 13, 2020 at 11:59

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.