Using WordPress ‘generic_ping()’ PHP function

The generic_ping WordPress PHP function sends pings to all of the ping site services for a specific post.

Usage

generic_ping( $post_id );

Parameters

  • $post_id (int) – Required. The ID of the post you want to ping.

More information

See WordPress Developer Resources: generic_ping

Examples

Ping a New Post

When you publish a new post and want to notify ping services:

// Notify ping services for post with ID 42
generic_ping( 42 );

Automatically Ping on Post Status Transition

Ping services when a post transitions from draft to publish:

add_action( 'transition_post_status', 'my_ping_on_publish', 10, 3 );

function my_ping_on_publish( $new_status, $old_status, $post ) {
    if ( 'publish' === $new_status && 'publish' !== $old_status ) {
        generic_ping( $post->ID );
    }
}

Ping Custom Post Type

Ping services for a custom post type, e.g., ‘product’:

add_action( 'publish_product', 'ping_custom_post_type', 10, 2 );

function ping_custom_post_type( $ID, $post ) {
    generic_ping( $ID );
}

Manually Ping for a Range of Post IDs

Ping services for a range of post IDs:

function manual_ping_range( $start_id, $end_id ) {
    for ( $i = $start_id; $i <= $end_id; $i++ ) {
        generic_ping( $i );
    }
}

// Ping posts with IDs from 100 to 110
manual_ping_range( 100, 110 );

Ping Services for All Published Posts

Ping services for all published posts:

function ping_all_published_posts() {
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'fields' => 'ids',
    );
    $posts = get_posts( $args );

    foreach ( $posts as $post_id ) {
        generic_ping( $post_id );
    }
}

// Notify ping services for all published posts
ping_all_published_posts();