Using WordPress ‘do_trackbacks()’ PHP function

The do_trackbacks() WordPress PHP function carries out trackbacks on a given post.

Usage

do_trackbacks( $post );

In this example, $post is the ID or object of the post you wish to perform trackbacks on.

Parameters

  • $post int|WP_Post (Required): This is the Post ID or object where the trackbacks will be performed.

More information

See WordPress Developer Resources: do_trackbacks()

The function was introduced in WordPress 1.5.0. No deprecation has been noted as of the last update (WordPress 5.8.1).

Examples

Perform Trackbacks on a Post with ID 42

This example shows how to perform trackbacks on a post with a known ID.

$post_id = 42; // Assigning a post ID
do_trackbacks( $post_id ); // Performing trackbacks on the post

Perform Trackbacks Using a WP_Post Object

In this case, we’re fetching a post object and then performing trackbacks on it.

$post = get_post( 42 ); // Fetching a post object
do_trackbacks( $post ); // Performing trackbacks on the post

Perform Trackbacks on the Latest Post

Here, we fetch the latest post and perform trackbacks on it.

$recent_post = wp_get_recent_posts( array( 'numberposts' => '1' ) )[0]; // Fetching the most recent post
do_trackbacks( $recent_post['ID'] ); // Performing trackbacks on the post

Perform Trackbacks on a Post Within The Loop

This example performs trackbacks on a post within The Loop.

if ( have_posts() ) : 
    while ( have_posts() ) : the_post();
        do_trackbacks( get_the_ID() ); // Performing trackbacks on the post
    endwhile; 
endif;

Perform Trackbacks on a Post from a Custom Post Type

In this case, we’re fetching a post from a custom post type and then performing trackbacks on it.

$args = array( 'post_type' => 'my_custom_post_type', 'posts_per_page' => 1 );
$custom_post = get_posts( $args )[0]; // Fetching a post from a custom post type
do_trackbacks( $custom_post->ID ); // Performing trackbacks on the post