0

How to get the same post content for in (the default) domain.com/year/month/post-slug and domain.com/custom/post-slug (without redirect).

Can I do this with some kind of rewrite or do I need a custom post type with it's own permalink structure?

So the gist is that I want to show the same content a little differently in different urls. Is this even possible? Any other ideas how to implement something like this?

1

I ended going the CPT route, as I couldn't figure out other way. Just added a bunch of logic to duplicate/modify/delete the normal posts when needed.

<?php // function for the CPT
function SU_kuuma_kysymys_type() { 
    // creating (registering) the custom type 
    register_post_type( 'kuuma_kysymys', /* (http://codex.wordpress.org/Function_Reference/register_post_type) */
        // let's now add all the options for this post type
        array( 'labels' => array(
            'name' => ( 'Kuumat kysymykset' ), /* This is the Title of the Group */
            'singular_name' => ( 'Kuuma kysymys' ), /* This is the individual type */
            'all_items' => ( 'Kaikki kysymykset' ), /* the all items menu item */
            'add_new' => ( 'Lisää uusi' ), /* The add new menu item */
            'add_new_item' => ( 'Add New Custom Type' ), /* Add New Display Title */
            'edit' => ( 'Edit' ), /* Edit Dialog */
            'edit_item' => ( 'Muokkaa kysymystä' ), /* Edit Display Title */
            'new_item' => ( 'Lisää kysymys' ), /* New Display Title */
            'view_item' => ( 'Näytä kysymys' ), /* View Display Title */
            'search_items' => ( 'Etsi kysymyksiä' ), /* Search Custom Type Title */ 
            'not_found' =>  ( 'Kysymyksiä ei löytynyt.' ), /* This displays if there are no entries yet */ 
            'not_found_in_trash' => ( 'Roskakori on tyhjä.' ), /* This displays if there is nothing in the trash */
            ), /* end of arrays */
            'public' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => true,
            'show_ui' => false,
            'show_in_admin_bar' => false,
            'query_var' => true,
            'menu_position' => 8, /* this is what order you want it to appear in on the left hand side menu */ 
            'menu_icon' => get_stylesheet_directory_uri() . '/library/images/custom-post-icon.png', /* the icon for the custom post type menu */
            'rewrite'   => array( 'slug' => 'kuuma-kysymys', 'with_front' => false ), /* you can specify its url slug */
            'has_archive' => 'kuuma-kysymys', /* you can rename the slug here */
            /* the next one is important, it tells what's enabled in the post editor */
            'supports' => array( 'title', 'editor', 'author', 'comments')
        ) /* end of options */
    ); /* end of register post type */

    /* this adds your post categories to your custom post type */
    register_taxonomy_for_object_type( 'category', 'custom_type' );
}
// adding the function to the Wordpress init
add_action('init', 'SU_kuuma_kysymys_type');


//first all the methods handling the question CPT posts
function SU_add_kysymys_post($post) {
    $newpost = array(
        'post_name'         => $post->post_name,
        'post_title'        => $post->post_title,
        'post_content'  => $post->post_content,
        'post_status'       => $post->post_status,
        'post_type'         => 'kuuma_kysymys',
        'post_author'       => $post->post_status,
        'post_category' => $post->post_category
    );

    $kysymys_post_id = wp_insert_post($newpost, true);

    // add the post ID's to their respective meta fields on both posts
    update_post_meta($post->ID, 'question_post_id', $kysymys_post_id);
    update_post_meta($kysymys_post_id, 'original_post_id', $post->ID);
}

function SU_update_kysymys_post($post, $q_post_id) {
    $updatedpost = array(
        'ID'                        => $q_post_id,
        'post_name'         => $post->post_name,
        'post_title'        => $post->post_title,
        'post_content'  => $post->post_content,
        'post_status'       => $post->post_status,
        'post_type'         => 'kuuma_kysymys',
        'post_author'       => $post->post_status,
        'post_category' => $post->post_category
    );
    wp_update_post($updatedpost, true);
}

function SU_delete_kysymys_post($post, $q_post_id = false) {
    // If we dont give $q_post_id check if we have the meta field
    if (!$q_post_id) { $q_post_id = get_post_meta($post_id, 'question_post_id', true); }
    // If we now have CPT post id -> delete it
    if($q_post_id != "") {
        update_post_meta($post->ID, 'question_post_id', '');
        // Remove the deletion hook in case we're just removing the category
        remove_action('delete_post', 'SU_check_kysymys_deletion', 10);
        wp_delete_post($q_post_id, true);
        add_action('delete_post', 'SU_check_kysymys_deletion', 10);
    }
}

// and the logic on what to do and when
function SU_check_kysymys($post_id, $post, $update) {
    //if new post or doing autosave/revision do nothing
    if(wp_is_post_revision($post_id) || wp_is_post_autosave($post_id) || get_post_status($post_id) == 'auto-draft') { return; }
  else {
    // We're saving/updating, go for it
    $q_post_id = get_post_meta($post_id, 'question_post_id', true);
    if (has_category('kuuma-kysymys', $post_id)) { // has the category "kuuma kysymys"
        if($q_post_id == "") {
                SU_add_kysymys_post($post);
            // wp_die('<pre>No q_post_id meta so new post?.<br><br>$post = '.var_export($update, true).'</pre>');       
        } else {
            SU_update_kysymys_post($post, (int)$q_post_id);
                // wp_die('<pre>We have q_post_id so just update existing CP I think.<br><br>$post = '.var_export($post, true).'</pre>'); 
           }
    } else { // doesn't have the category (at least not anymore)

        if($q_post_id == "") { return; } // doesn't have the category or earlier remains of the meta field
        else { // doesn't have the category but has meta field -> had the category before -> delete the added CP
            SU_delete_kysymys_post($post, (int)$q_post_id);
                //wp_die('<pre>We have post meta but lack the category so delete CP<br><br>$post = '.var_export($post, true).'</pre>'); 
        }
    }
  }
}
add_action('save_post', 'SU_check_kysymys', 10, 3);

// logic for deleting questions if the concurrent regular post is deleted for any reason
function SU_check_kysymys_deletion($post) {
    if (has_category('kuuma-kysymys', $post->ID)) {
        SU_delete_kysymys_post($post);
    }
}
add_action('delete_post', 'SU_check_kysymys_deletion', 10); ?>

but this way I can access the "same" post from two different locations and have a custom template for each one.

-2

I think you need to change your permalink structure for all your past posts and future posts?

Just go to setting-> Permalink and type in custom permalink there, Now secondly I would recommend you to use Permalink Finder Plugin (Not Updated but is not having any compatibility issues), it will automatically redirect your posts to the changed permalink...I have been using it and it always 100% redirects correctly.

  • 1
    Don't spam please, next post I will flag as spam. Please refrain from using answers to post links to your site – Pieter Goosen Apr 28 '15 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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