Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have publications and I want to simply add year and month to the permalink. Currently the year and month of publication is added to the post as a meta value. I've set up publications as a custom post type and created taxonomy for the specific title of the publication. I would like the permalink to be:

publications/publication-title/year-published/month-published/name-of-post

I simply want to add the meta values for the publication year and month to the permalink.

So far, I've been able to pull the year and month published and add it to the url but unfortunately I keep getting a 404 when I try to access posts. I think I'm taking a wrong turn with one of my steps below ...

Any help would be appreciated.

These are the steps I did in the following order:

  1. Since I was adding publication-year and publication-month to the url, I was reading that I had to make WordPress aware of these custom querystring variables. I added the following functions to register these custom variables:

    function pubyear_register_rewrite_tag() {  
        add_rewrite_tag( '%pubyear%', '([0-9]{4})');  
    }  
    
    add_action( 'init', 'pubyear_register_rewrite_tag');
    
    function pubmonth_register_rewrite_tag() {  
       add_rewrite_tag( '%pubmonth%', '([0-9]{2}');  
    }  
    
    add_action( 'init', 'pubmonth_register_rewrite_tag');
    

2) Added the year and month to the permalink structure:

add_filter('post_type_link', 'pub_term_permalink', 10, 4);

function pub_term_permalink($permalink, $post, $leavename, $sample)
{
    if ( false !== strpos( $permalink, '%publication-title%/%pubyear%/%pubmonth%' ) ) {

     //get the publication-title
     $publicationtype = get_the_terms( $post->ID, 'publication-type' );

     //get the year of the publication
     $pubyear = date('Y', get_post_meta($post->ID, 'pub_date', true));

     //get the month of the publication
     $pubmonth = date('m', get_post_meta($post->ID, 'pub_date', true));

     $rewritecode = array(
         '%publication-type%',
         '%pubyear%',
         '%pubmonth%',
         $leavename? '' : '%postname%',
     );

     $rewritereplace = array(
         array_pop($publicationtype)->slug,
         $pubyear,
         $pubmonth,
         $post->post_name
     );

     $permalink = str_replace($rewritecode, $rewritereplace, $permalink);    

 }
    return $permalink;
}

3) Add rewrite rules:

function pub_add_rewrite_rules() {  
    add_rewrite_rule(  '^([^/]*)/([0-9]{4})/([0-9]{2})/([^/]+)?', 'publications/index.php?pagename=$matches[3]', 'top' );  
}  

add_action( 'init', 'pub_add_rewrite_rules' );

4) Made sure the custom post type is registered and includes this rewrite array:

'rewrite' => array
    (
            'slug' => 'publications/%publication-type%/%pubyear%/%pubmonth%',
    'with_front' => false
         ),
share|improve this question
    
I found a nice article about this. I tried it on an educational site on post type courses to make the URLs like: example.com/lecturer-name/course-name. –  borantula Nov 14 '12 at 13:40

1 Answer 1

Was able to get this working. Thought I would write down what we did in the hopes that it will help someone in the future (or someone can give me feedback on how to better improve it!)

  1. Registered Custom Rewrite Rules

    add_action('init', 'pub_rewrite_rules');
    
    function pub_rewrite_rules()
    {
    global $wp_rewrite;
    
    $wp_rewrite->add_rewrite_tag( '%pubyear%', '([0-9]{4})', 'pubyear=');
    $wp_rewrite->add_rewrite_tag( '%pubmonth%', '([0-9]{2})', 'pubmonth=');
    }
    
  2. Created Permalink Structure

     function pub_permalink($permalink, $post, $leavename)
    {
    if ( false !== strpos( $permalink, '%publication-type%/%pubyear%/%pubmonth%' ) ) {
    
    $publicationtype = get_the_terms($post->ID, 'publication-type');
    $pubyear = date('Y', get_post_meta($post->ID, 'publication_date', true));
    $pubmonth = date('m', get_post_meta($post->ID, 'publication_date', true));
    
    $rewritecode = array(
     '%publication-type%',
     '%pubyear%',
     '%pubmonth%',
     $leavename? '' : '%postname%',
    );
    
    $rewritereplace = array(
     array_pop($publicationtype)->slug,
     $pubyear,
     $pubmonth,
     $post->post_name
    );
    
    $permalink = str_replace($rewritecode, $rewritereplace, $permalink);    
        }
           return $permalink;
    }
    
  3. Registered Taxonomy

  4. Registered Custom Post Type wrote rewrite array to include:

     'slug' => 'publications/%publication-type%/%pubyear%/%pubmonth%',
    
  5. flush rewrite rules by going to Permalink Settings page and saving or flush_rewrite_rule()

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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