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'm about to create parser which would be inserting new custom posts. So, it's pretty simple

global $user_ID;
$new_post = array(
    'post_title' => 'My New Post',
    'post_content' => 'Lorem ipsum dolor sit amet...',
    'post_status' => 'publish',
    'post_date' => date('Y-m-d H:i:s'),
    'post_author' => $user_ID,
    'post_type' => 'post',
    'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);

But i have some extra logic in creating permalinks.

function my_post_type_link_filter_function( $post_link, $id = 0, $leavename = FALSE ) {
    if ( strpos('%dgor%', $post_link) === 'FALSE' && strpos('%znak%', $post_link) === 'FALSE' ) {
        return $post_link;
    }
    $post = get_post($id);
    if ( !is_object($post) || $post->post_type != 'goroskop' ) {
        return $post_link;
    }
    $day = wp_get_object_terms($post->ID, 'gday');
    $month = wp_get_object_terms($post->ID, 'gmonth');
    $year = wp_get_object_terms($post->ID, 'gyear');
    if (  !$day || !$month || !$year ) {
        return $post_link;
    }
    $post_link = str_replace('%gday%', $day[0]->slug, $post_link);
    $post_link = str_replace('%gmonth%', $month[0]->slug, $post_link);
    return str_replace('%gyear%', $year[0]->slug, $post_link);
}
add_filter('post_type_link', 'my_post_type_link_filter_function', 1, 3);

So, i need to replace some markers in my permalink structure based on custom taxonomy.

The question is: Is there any way to invoke this logic programmatically while inserting new custom post?

share|improve this question
    
post_type_link will only be triggered when displaying permalink for a post with a custom post type. So it doesn't make sense that you want to change the permalink when inserting a post. –  Jesse May 6 '13 at 1:21

1 Answer 1

add_action( 'publish_post', 'run_when_post_published_first_time',10,2 );
function run_when_post_published_first_time($post_id, $post)
{
// Checks whether is post updated or published at first time.
if ($post->post_date != $post->post_modified) return;

// Place here your code
}

from http://wordpress.org/support/topic/new-post-hook

share|improve this answer
    
My filter above have link to the post_link. How can i change permalink here? –  Anton Abramov Nov 16 '12 at 12:34
    
try this filter add_filter('post_link', 'my_post_type_link_filter_function', 10, 3); when you add your post using your code it will be get called. –  Mubbashar Nov 16 '12 at 12:57
    
I've tried to insert post, but permalink markers still there. I think i need something to force execution filter, but i don't know how –  Anton Abramov Nov 16 '12 at 13:16
    
Btw i tried the code and it run the filter. Can you try it by adding exit in the function or printing something so you will be sure that the filter is executing. –  Mubbashar Nov 16 '12 at 13:31
    
do you mean put add_filter to the publish_post action like you wrote above? –  Anton Abramov Nov 16 '12 at 14:29

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.