Using WordPress ‘get_to_ping()’ PHP function

The get_to_ping WordPress PHP function retrieves a list of URLs that need to be pinged.

Usage

get_to_ping($post);

Custom example:

$post_ID = 45;
$urls_to_ping = get_to_ping($post_ID);

Parameters

  • $post (int|WP_Post) – Required. Post ID or post object.

More information

See WordPress Developer Resources: get_to_ping

Examples

Retrieve URLs to ping for a specific post

Retrieve the list of URLs to ping for the post with the ID 45 and display them as an unordered list.

$post_ID = 45;
$urls_to_ping = get_to_ping($post_ID);

echo '<ul>';
foreach ($urls_to_ping as $url) {
    echo '<li>' . $url . '</li>';
}
echo '</ul>';

Check if a specific URL is in the ping list

Check if a specific URL is in the ping list of the post with the ID 32.

$post_ID = 32;
$target_url = 'https://example.com';
$urls_to_ping = get_to_ping($post_ID);

if (in_array($target_url, $urls_to_ping)) {
    echo 'URL found in ping list.';
} else {
    echo 'URL not found in ping list.';
}

Count URLs to ping for a specific post

Count the number of URLs that need to be pinged for the post with the ID 27.

$post_ID = 27;
$urls_to_ping = get_to_ping($post_ID);

echo 'Number of URLs to ping: ' . count($urls_to_ping);

Remove a specific URL from the ping list

Remove a specific URL from the ping list of the post with the ID 18.

$post_ID = 18;
$target_url = 'https://example.com';
$urls_to_ping = get_to_ping($post_ID);

$filtered_urls = array_diff($urls_to_ping, [$target_url]);

// Update the ping list with the filtered URLs.
update_post_meta($post_ID, '_pingme', implode("\n", $filtered_urls));

Add a new URL to the ping list

Add a new URL to the ping list of the post with the ID 55.

$post_ID = 55;
$new_url = 'https://newsite.com';
$urls_to_ping = get_to_ping($post_ID);

$updated_urls = array_unique(array_merge($urls_to_ping, [$new_url]));

// Update the ping list with the updated URLs.
update_post_meta($post_ID, '_pingme', implode("\n", $updated_urls));