I want to avoid duplicate post content via xmlrpc. So:
if post exists nothing happens… .
if does not exists data insertion occurs… .
The method for checking would be post title or post content.
I saw this solution but it seems not to work.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Since you add a reference to my previous answer, let me share how I tested it:
Setup on site A – XML-RPC Client
include_once( ABSPATH . WPINC . '/class-IXR.php' );
include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' );
$client = new WP_HTTP_IXR_CLIENT( 'http://example.tld/xmlrpc.php' ); // <-- Change!
$client->debug = true;
$result = $client->query(
'wp.newPost',
[
0,
"username", //<-- Change!
"password", //<-- Change!
[
'post_status' => 'draft',
'post_title' => 'xml-rpc testing',
'post_content' => 'hello xml-rpc! Random: ' . rand( 0, 999 ),
]
]
);
where you have to modify the path, username and password to your needs.
If I recall correctly, this great article by Eric Mann helped me regarding the client setup code, when I tested my plugin last year.
Setup on site B – XML-RPC Server
Here we add the following plugin:
<?php
/**
* Plugin Name: Avoid XML-RPC Post Title Duplication
* Description: Prevent duplicate posts when doing wp.newPost via XML-RPC
* Plugin URI: http://wordpress.stackexchange.com/a/157261/26350
*/
add_action ('xmlrpc_call', 'wpse_xmlrpc_call' ); /////
function wpse_xmlrpc_call( $method )
{
if( 'wp.newPost' === $method )
add_filter( 'xmlrpc_wp_insert_post_data', 'wpse_xmlrpc_wp_insert_post_data' );
}////
function wpse_xmlrpc_wp_insert_post_data( $post_data )
{
// Check if the post title exists:
$tmp = get_page_by_title(
$post_data['post_title'],
OBJECT,
$post_data['post_type']
);
// Go from 'insert' to 'update' mode within wp_insert_post():
if( is_object ( $tmp ) )
$post_data['ID'] = $tmp->ID;
return $post_data;
}
Tests
Before activating our plugin:
If client A creates three posts with the same title, but different content, then they will show up like this on site B:
Here we see that those three posts are all created on the server B as new posts.
After activating our plugin:
Now if client A creates a post, then it will show up on server B like this:
Then client A creates another post, with the same title, but different content. Now the previous post is modified:
The post list will show up like this:
so we have avoided post duplication.
Notes
Make sure site B has XML-RPC enabled.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0



