• hello, i’m interested how to run a piece of code, when the post is being published. I was trying placing this in functions.php but it didnt work:

    function stuff($post_id) {

    global $wpdb;
    $numposts = $wpdb->get_var(“SELECT COUNT(*) FROM $wpdb->posts WHERE $wpdb->posts.post_status = ‘publish’ AND $wpdb->posts.post_type = ‘post'”);
    if (0 < $numposts) $numposts = number_format($numposts);
    $numposts++;

    $tempmeta = get_post_meta($post_id, ‘Author Name’, true);
    add_post_meta($post_id, ‘authkey’, “$tempmeta”);

    $postdata = array(
    “ID” => $post_id,
    “post_title” => “$numposts”,
    “post_name” => “$numposts”);
    sanitize_post($postdata,$wpdb);
    wp_update_post($postdata);

    } //end stuff
    add_action( ‘publish_post’, ‘stuff’ );

    what I want to do is to change the title and slug of the post automatically when it is published. The name has to be the number of total posts+1. Maybe there is an easier way.. I’m not a pro in coding, please help me ??

    when the post is being published. If I make it like

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Run a piece of code when post is published’ is closed to new replies.