カスタム投稿タイプ使用時のパーマリンクをPostnameからPost_Idにする

新しい記事「カスタム投稿タイプ使用時のパーマリンクをPostnameからPost_Idにする(2)」があります。


カスタム投稿タイプで投稿した内容のパーマリンクは「/post_type/postname」が標準でスラングを使用していないとタイトルがURLになる。wikiのような表示と言えば分りやすいだろうか。
他のページはpost_id表示しているので、これもpost_id表示にしたい・・・


// function.php
add_action('init', 'myposttype_rewrite');
function myposttype_rewrite() {
    global $wp_rewrite;
    $queryarg = 'post_type=myposttype&p=';
    $wp_rewrite->add_rewrite_tag('%post_id%', '([^/]+)', $queryarg);
    $wp_rewrite->add_permastruct('myposttype', '/myposttype/%post_id%', false);
}

add_filter('post_type_link', 'myposttype_permalink', 1, 3);
function myposttype_permalink($post_link, $id = 0, $leavename) {
    global $wp_rewrite;
    $post = &get_post($id);
    if ( is_wp_error( $post ) )
        return $post;
    $newlink = $wp_rewrite->get_extra_permastruct('myposttype');
    $newlink = str_replace("%post_id%", $post->ID, $newlink);
    $newlink = home_url(user_trailingslashit($newlink));
    return $newlink;
}

「myposttype」のところを自分用のカスタム投稿名に変更するとpost_idで表示される。
複数の時はどうする?
※テストするとpost_type名の指定が悪いのかわからないが、404になる時がある。


上のコードとほぼ同じだが、こちらは、「/カスタム投稿名/年/月/日/投稿id」になるらしい。

// function.php
class set_custom_rewrite {
    var $post_type;

    function set_custom_rewrite($post_type){
        $this->post_type = $post_type;

        add_action('init', array(&$this,'set_rewrite'));
        add_action('init', array(&$this,'add_rule'));
        add_filter('post_type_link', array(&$this,'set_permalink'), 1, 3);
    }
    //Rewrite Ruleの追加
    function add_rule(){
        add_rewrite_rule($this->post_type.'/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type='.$this->post_type);
        add_rewrite_rule($this->post_type.'/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type='.$this->post_type);
        add_rewrite_rule($this->post_type.'/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$','index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]&post_type='.$this->post_type);
        add_rewrite_rule($this->post_type.'/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$','index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&post_type='.$this->post_type);
        add_rewrite_rule($this->post_type.'/([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type='.$this->post_type);
        add_rewrite_rule($this->post_type.'/([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type='.$this->post_type);
        add_rewrite_rule($this->post_type.'/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$','index.php?year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]&post_type='.$this->post_type);
        add_rewrite_rule($this->post_type.'/([0-9]{4})/([0-9]{1,2})/?$','index.php?year=$matches[1]&monthnum=$matches[2]&post_type='.$this->post_type);
        add_rewrite_rule($this->post_type.'/([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&feed=$matches[2]&post_type='.$this->post_type);
        add_rewrite_rule($this->post_type.'/([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$','index.php?year=$matches[1]&feed=$matches[2]&post_type='.$this->post_type);
        add_rewrite_rule($this->post_type.'/([0-9]{4})/page/?([0-9]{1,})/?$','index.php?year=$matches[1]&paged=$matches[2]&post_type='.$this->post_type);
        add_rewrite_rule($this->post_type.'/([0-9]{4})/?$','index.php?year=$matches[1]&post_type='.$this->post_type);
    }

    function set_rewrite() {
        global $wp_rewrite;

        $queryarg =  'post_type='.$this->post_type.'&p=';
        $wp_rewrite->add_rewrite_tag('%'.$this->post_type.'_id%', '([^/]+)',$queryarg);
        $wp_rewrite->add_permastruct($this->post_type, '/'.$this->post_type.'/%year%/%monthnum%/%day%/%'.$this->post_type.'_id%/', false);
    }

    function set_permalink($post_link, $id = 0) {
        global $wp_rewrite;

        $post = &get_post($id);
        if ( is_wp_error( $post ) )
            return $post;

        $newlink = $wp_rewrite->get_extra_permastruct($this->post_type);
        $newlink = str_replace("%".$this->post_type."%", $post->post_type, $newlink);
        $newlink = str_replace("%".$this->post_type."_id%", $post->ID, $newlink);

        $post_date = strtotime($post->post_date);
        $post_year = date("Y",$post_date);
        $post_monthnum = date("m",$post_date);
        $post_day = date("d",$post_date);

        $newlink = str_replace("%year%",$post_year, $newlink);
        $newlink = str_replace("%monthnum%",$post_monthnum, $newlink);
        $newlink = str_replace("%day%",$post_day, $newlink);

        $newlink = home_url(user_trailingslashit($newlink));
        return $newlink;
    }

}

$set_blog_permalink = new set_custom_rewrite("カスタム投稿名");

「/%post_type%/%taxonomy%/%term%」で表示。

function custom_init() {
    global $wp_rewrite;

    // Declare our permalink structure
    $post_type_structure = '/%post_type%/%taxonomy%/%term%';

    // Make wordpress aware of our custom querystring variables
    $wp_rewrite->add_rewrite_tag("%post_type%", '([^/]+)', "post_type=");
    $wp_rewrite->add_rewrite_tag("%taxonomy%", '([^/]+)', "taxonomy=");
    $wp_rewrite->add_rewrite_tag("%term%", '([^/]+)', "term=");

    // Only get custom and public post types
    $args=array(
        'public'   => true,
        '_builtin' => false
    );
    $output = 'names'; // names or objects, note names is the default
    $operator = 'and'; // 'and' or 'or'
    $post_types=get_post_types($args,$output,$operator);
    $post_types_string = implode("|", $post_types);

    $taxonomies=get_taxonomies($args,$output,$operator); // Note the use of same arguments as with get_post_types()
    $taxonomies_string = implode("|", $taxonomies);

    // Now add the rewrite rules, note that the order in which we declare them are important
    add_rewrite_rule('^('.$post_types_string.')/('.$taxonomies_string.')/([^/]*)/?','index.php?post_type=$matches[1]&$matches[2]=$matches[3]','top');
    add_rewrite_rule('^('.$post_types_string.')/([^/]*)/?','index.php?post_type=$matches[1]&name=$matches[2]','top');
    add_rewrite_rule('^('.$post_types_string.')/?','index.php?post_type=$matches[1]','top');

    // Finally, flush and recreate the rewrite rules
    flush_rewrite_rules();
}

function post_type_permalink($permalink, $post_id, $leavename){
    $post = get_post($post_id);

    // An array of our custom query variables
    $rewritecode = array(
        '%post_type%',
        '/%taxonomy%',
        '/%term%',
        $leavename? '' : '%postname%',
        $leavename? '' : '%pagename%',
    );

    // Avoid trying to rewrite permalinks when not applicable
    if ('' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft'))) {
        // Fetch the post type
        $post_type = get_post_type( $post->ID );

        // Setting these isn't necessary if the taxonomy has rewrite = true,
        // otherwise you need to fetch the relevant data from the current post
        $taxonomy = "";
        $term = "";

        // Now we do the permalink rewrite
        $rewritereplace = array(
            $post_type,
            $taxonomy,
            $term,
            $post->post_name,
            $post->post_name,
        );
        $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
    }

    return $permalink;
}

// Create custom rewrite rules
add_action('init', 'custom_init');

// Translate the custom post type permalink tags
add_filter('post_type_link', 'post_type_permalink', 10, 3);

他にも

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)