0

I am using Simple Urls to setup redirects on my blog. I am trying to create redirects automatically upon publishing a post. For example, If i publish a post about StudioPress it will be automatically publish a redirect with Title similar to blog post. i.e StudioPress. and the "URL" custom field will update the Redirect url in Simple URLs Plugin.

In simple terms, i want to clone blog post for "surl" (CPT name of Simple URLS Plugin) post type with similar title and for redirect url i will setup a Custom Field Named "URL".

I am somehow successful in cloning the post title but the redirect url is not copying. Here is my code. Please have a look and correct the error for Custom Field URL.

add_action( 'save_post', 'save_postdata_wpse_76945', 10, 2 );
function save_postdata_wpse_76945( $post_id, $post_object )
{
// Auto save?
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    return;

// Correct post_type
if ( 'post' != $post_object->post_type )
    return;

// Security
if (
    !isset($_POST['noncename_wpse_76945'])
    || !wp_verify_nonce( $_POST['noncename_wpse_76945'], plugin_basename( __FILE__ ) )
    )
    return;

// Prepare contents
$add_cpt_clone = array(
                'post_title'   => $post_object->post_title,
                '_surl_redirect' => get_post_meta( get_the_ID(), 'url' ),
                'post_status'  => 'publish',
                'post_type'    => 'surl'
              );

// Insert the post into the database
$p_id = wp_insert_post( $add_cpt_clone );

// Use $p_id to insert new terms
}

I know i am doing some mistake here

'_surl_redirect' => get_post_meta( get_the_ID(), 'url' ),

But i don't know how to correct it. I am using ACF for generating custom field URL.

Any Ideas. It's very urgent and i am using Genesis and therefore, i don't want to use any other plugin that is not done by Genesis team. Regards

2 Answers 2

0

Something like this should work-

if( get_post_meta( $post_id, '_surl_post_id', true ) ) {
    // we have an old post created here
    update_post_meta( get_post_meta( $post_id, '_surl_post_id', true ), '_surl_redirect', get_post_meta( $post_id, 'url', true ) );
    return;
}

// Prepare contents
$add_cpt_clone = array(
                'post_title'   => $post_object->post_title,
                'post_status'  => 'publish',
                'post_type'    => 'surl'
              );

// Insert the post into the database
$p_id = wp_insert_post( $add_cpt_clone );

// Insert the meta key for redirect
update_post_meta( $p_id, '_surl_redirect', get_post_meta( $post_id, 'url', true ) );
// save the id for future use
update_post_meta( $post_id, '_surl_post_id', $p_id );

// Insert any other meta keys that might be needed
24
  • Thanks a lot Mridul Aggarwal for solving the prior issue, I am now facing another issue. After Publishing a post when i update the custom field URL, it doesn't get updated in the Simple URLs Plugin. How can i do that as well?
    – Rogger
    Commented Feb 24, 2016 at 23:46
  • That should be possible using a save_post hook. But you also need to establish a link between the earlier post and the newly created post. You could use update_post_meta( $post_id, '_surl_post_id', $p_id ) to save the new post's id with the old post's meta. Then create a separate hook on save_post action, which will check if the meta was updated and update the other post's meta if it did. Commented Feb 25, 2016 at 18:59
  • save_post will not handle any programmatically applied updates, so if that's a concern, you might want to look into the hooks available in function update_metadata() Commented Feb 25, 2016 at 19:01
  • Hi Mridul, I am not that good in PHP and hooks. Can you please update the above code so that i can paste that in my functions file. I will be obliged. Thanks
    – Rogger
    Commented Feb 25, 2016 at 23:22
  • Hey, i have updated some untested code, i think it should work. Also note that it will work on newly created posts, not the old ones. Commented Feb 26, 2016 at 19:51
0

I tried the above code. It completely stopped working. Can you please recheck it once if i am pasting the full code correctly. Thanks

add_action( 'save_post', 'save_postdata_wpse_76945', 10, 2 );
function save_postdata_wpse_76945( $post_id, $post_object )
{
// Auto save?
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    return;

// Correct post_type
if ( 'post' != $post_object->post_type )
    return;

// Security
if (
    !isset($_POST['noncename_wpse_76945'])
    || !wp_verify_nonce( $_POST['noncename_wpse_76945'], plugin_basename( __FILE__ ) )
    )
    return;

if( get_post_meta( $post_id, '_surl_post_id', true ) ) {
    // we have an old post created here
    update_post_meta( get_post_meta( $post_id, '_surl_post_id', true ), '_surl_redirect', get_post_meta( $post_id, 'url', true ) );
    return;
}

// Prepare contents
$add_cpt_clone = array(
                'post_title'   => $post_object->post_title,
                'post_status'  => 'publish',
                'post_type'    => 'surl'
              );

// Insert the post into the database
$p_id = wp_insert_post( $add_cpt_clone );

// Insert the meta key for redirect
update_post_meta( $p_id, '_surl_redirect', get_post_meta( $post_id, 'url', true ) );
// save the id for future use
update_post_meta( $post_id, '_surl_post_id', $p_id );

// Insert any other meta keys that might be needed

}

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.