Custom function counts posts but only up to 10

I'm trying to find a way to count publish posts and then saves them to a custom field and updates the permalink. My permalink structure is saved as '/episode/$number'. I think the best way for this is to use 'wp_insert_post'.

The code below works until the 10th post; after that, every post gets counted as 10, 10 is then saved to the custom field and all permalinks get saved as 10-2.

add_action( 'init', function() {
    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure( '/episode/%postname%/' );
});
function updateNumbers( $data, $postarr ) {
    if ( !empty( $postarr['ID'] ) && ! wp_is_post_revision( $postarr['ID'] ) ) {
        if ( !( $counts = get_post_meta( $postarr['ID'], 'incr_number', true ) ) ) {
            global $wpdb;
            $counts = (int)$wpdb->get_var( "SELECT MAX(meta_value) FROM {$wpdb->postmeta} WHERE meta_key='incr_number'" );
            $counts++;
            update_post_meta( $postarr['ID'], 'incr_number', $counts );
        }
        $data['post_name'] = $counts;
    }
    return $data;
    }
add_filter('wp_insert_post_data', 'updateNumbers', 10, 2 );
  • Ash
    • WordPress Hacker

    Hello Gregory

    Would you please try the function like this?

    function updateNumbers( $data, $postarr ) {
        if ( !empty( $postarr['ID'] ) && ! wp_is_post_revision( $postarr['ID'] ) ) {
    
                global $wpdb;
                $counts = (int)$wpdb->get_var( "SELECT MAX(meta_value) FROM {$wpdb->postmeta} WHERE meta_key='incr_number'" );
                $counts++;
                update_post_meta( $postarr['ID'], 'incr_number', $counts );
                $data['post_name'] = $counts;
        }
        return $data;
    }

    Run it only once. And then check if the slug gets correct number.

    Please let me know if it works. Have a nice day!

    Cheers,
    Ash

  • Gregory
    • Site Builder, Child of Zeus

    UPDATE: I got it to work but I have a minor problem.

    function updateNumbers( $data, $postarr ) {
        if ( !empty( $postarr['ID'] ) && ! wp_is_post_revision( $postarr['ID'] ) ) {
            if ( !( $counts = get_post_meta( $postarr['ID'], 'incr_number', true ) ) ) {
                global $wpdb;
                $counts = (int)$wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE (post_status = 'publish' AND post_type = 'post')");
                $counts++;
                update_post_meta( $postarr['ID'], 'incr_number', $counts );
            }
            $data['post_name'] = $counts;
        }
        return $data;
    }
    add_filter('wp_insert_post_data', 'updateNumbers', 10, 2 );

    Now the problem (minor) is that sometimes the permalink won't show up in the editor until the post is publish. Is there a way to force the editor to show the permalink?

    Example of the editor showing the updated permalink structure before the post is published.

    Example of the editor now showing the updated permalink structure. It's still working but doesn't show up until the post is published

  • Gregory
    • Site Builder, Child of Zeus

    Well a working solution is to move the function to a higher priority, I moved from:

    add_filter('wp_insert_post_data', 'updateNumbers', 10, 2 );
    to
    add_filter('wp_insert_post_data', 'updateNumbers', 1, 2 );

    While moving the function to a higher priority, some posts do see a delay in showing the permalink on the edit screen but they do show up, unlike the previous setting where it would only show up until the post is published.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.