最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Adding meta values to permalink

programmeradmin2浏览0评论

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
         ),

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 asked Aug 13, 2012 at 23:53 user1255062user1255062 811 silver badge5 bronze badges 1
  • I found a nice article about this. I tried it on an educational site on post type courses to make the URLs like: example/lecturer-name/course-name. – user23630 Commented Nov 14, 2012 at 13:40
Add a comment  | 

2 Answers 2

Reset to default 4

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()

when is pub_permalink() called? maybe you missed the action or filter?

EDIT : you missed this line:

add_filter('post_link', 'pub_permalink', 10, 3);
发布评论

评论列表(0)

  1. 暂无评论