Want to manually trigger Instant Indexing API in my function

#811275
  • Resolved Mr SKT
    Rank Math free

    Hello, I want to manually trigger instant indexing API in my custom PHP function just like save_post.

    Here my custom function code:

    function ga_qse_update_slug() {
    global $wpdb;

    $post_id = intval($_POST['post_id']);
    $new_slug = sanitize_title($_POST['new_slug']);

    if (empty($post_id) || empty($new_slug)) {
    wp_send_json_error('Invalid post ID or slug');
    }

    // Check if the post exists
    $post = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE ID = %d", $post_id));
    if (is_null($post)) {
    wp_send_json_error('Post not found');
    }

    // Update the post slug
    $result = $wpdb->update(
    $wpdb->posts,
    array('post_name' => $new_slug),
    array('ID' => $post_id),
    array('%s'),
    array('%d')
    );

    if ($result === false) {
    wp_send_json_error('Database error: ' . $wpdb->last_error);
    } elseif ($result === 0) {
    wp_send_json_error('Failed to update slug');
    } else {
    wp_send_json_success('Slug updated successfully');
    }
    }

    Just like when I update posts and it automatically submits to the Instant Indexing API, the same i want it hereafter wp_send_json_success('Slug updated successfully');

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    To manually trigger the Instant Indexing API in your custom PHP function after updating the post slug, you can add the following code snippet to your existing function:

    function ga_qse_update_slug() {
        global $wpdb;
    
        $post_id = intval($_POST['post_id']);
        $new_slug = sanitize_title($_POST['new_slug']);
    
        if (empty($post_id) || empty($new_slug)) {
            wp_send_json_error('Invalid post ID or slug');
        }
    
        // Check if the post exists
        $post = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE ID = %d", $post_id));
        if (is_null($post)) {
            wp_send_json_error('Post not found');
        }
    
        // Update the post slug
        $result = $wpdb->update(
            $wpdb->posts,
            array('post_name' => $new_slug),
            array('ID' => $post_id),
            array('%s'),
            array('%d')
        );
    
        if ($result === false) {
            wp_send_json_error('Database error: ' . $wpdb->last_error);
        } elseif ($result === 0) {
            wp_send_json_error('Failed to update slug');
        } else {
            // Trigger Instant Indexing API
            do_action( 'rank_math/ping-index' );
    
            wp_send_json_success('Slug updated successfully');
        }
    }

    In the updated function, after successfully updating the post slug, the do_action( 'rank_math/ping-index' ); line will trigger the Instant Indexing API to notify search engines about the changes made to the post.

    Please ensure that you have Rank Math SEO plugin installed and activated for this code to work correctly.

    If you encounter any issues or need further assistance, feel free to reach out.

    Sources:
    How to Automate the Insertion of Focus Keyword » Rank Math
    How to NoIndex All Posts in a Category » Rank Math

    Mr SKT
    Rank Math free

    I don’t think do_action( 'rank_math/ping-index' ); only this line will work. We have to it for specific URL (get permalink from $post_id) and then trigger it for that URL

    Hello,

    We don’t provide custom code for this situation.

    You can check what our plugin is doing by installing the Instant Indexing plugin from here: https://wordpress.org/plugins/fast-indexing-api/

    You would need to essentially recreate the entire submission part of the plugin into your code to be able to do this. We don’t have any filters or actions that can be used for this.

    At this point, it’s much easier to just use the automatic submission option of the plugin for that and don’t try to re-create the wheel.

    Don’t hesitate to get in touch if you have any other questions.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)

The ticket ‘Want to manually trigger Instant Indexing API in my function’ is closed to new replies.