4

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

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' ),
            ),
        )
    )
);
  • Thanks, I'll give it a shot. Just curious - how is tax_input different from post_category? I'm not using a custom post type or anything so couldnt I also use: $post_data['post_category'] = array(207) – user57040 Dec 11 '14 at 16:01
  • Yes that should also work, since post_category is supported by wp_insert_post(). – birgire Dec 12 '14 at 3:09
  • Did that work for you? – birgire Dec 17 '14 at 12:18
  • No unfortunately not. I'm not sure how to even debug it to see what could be going wrong. – user57040 Dec 17 '14 at 19:47
  • These methods worked fine when I tested them. You must make sure that the corresponding category exists. So you can create a post but not assign the category to it? – birgire Dec 25 '14 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.