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?

  • 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
  • Perhaps some context would be good? Are you putting links to post inside the content of the new post? – Tom J Nowell Dec 25 '14 at 11:00
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

  • 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

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.