I have a service that puts a daily post to my blog via XML-RPC. It doesn’t offer me any control of what category it goes into so I want to just add a filter/hook to add the category I want into the incoming post data:
add_action ('xmlrpc_call', 'check_xmlrpc_call' );
function check_xmlrpc_call( $method )
{
if( 'wp.newPost' === $method )
{
add_filter( 'xmlrpc_wp_insert_post_data', 'add_xmlrpc_category_post_data' );
}
}
function add_xmlrpc_category_post_data( $post_data )
{
//not sure how to add category to the post data :(
return $post_data;
}
I think something like this in my functions.php
would work? I just am not sure the correct format to add a category to the post data?
Maybe this?
$post_data['terms'] = array('category' => array(207))
1 Answer
Assigning posts to taxonomy terms in XML-RPC:
Let’s assume your setup is:
xml-rpc
wp.newPost
(sender) site A -------------> site B (receiver)
and you want to assign the new posts to a given taxonomy terms on site B.
From site B:
Then you can try the following, on the receiving site B:
$post_data['tax_input'] = array( 'category' => array( 207 ) );
where the category
taxonomy with id 207
already exists on the site B
.
It’s also possible to use the other supported parameters of wp_insert_post()
, like post_category
or tags_input
. In your case you could therefore also use:
$post_data['post_category'] = array( 207 );
From site A:
Notice that the terms
and terms_names
parameters are supported by the payload of the wp.newPost
query, from the sending
site A.
Here’s an example for the site A, how one could add terms of a given taxonomy:
$result = $client->query(
'wp.newPost',
array(
$blog_id,
$user,
$password,
array(
'post_status' => 'draft',
'post_title' => 'Test',
'post_content' => 'We are testing XML-RPC!',
'terms_names' => array(
'post_tag' => array( 'xml-rpc' ),
'category' => array( 'wordpress-testing' ),
),
)
)
);