Skip to content

Instantly share code, notes, and snippets.

@ssz360
Last active June 29, 2020 08:07
Show Gist options
  • Save ssz360/717cca0becf5768af2764b30246cd83f to your computer and use it in GitHub Desktop.
Save ssz360/717cca0becf5768af2764b30246cd83f to your computer and use it in GitHub Desktop.
wordpress: get and set post meta in xmlrpc
add_filter('xmlrpc_methods', 'my_xmlrpc_methods');
function my_xmlrpc_methods($methods)
{
$methods['ssz.addnew.post.meta'] = 'ssz_add_new_post_meta';
$methods['ssz.get.post.meta'] = 'ssz_get_post_meta';
return $methods;
}
function ssz_get_post_meta($args)
{
// Parse the arguments, assuming they're in the correct order
$blodID = $args[0];
$username = $args[1];
$password = $args[2];
$post_id = $args[3];
global $wp_xmlrpc_server;
// Let's run a check to see if credentials are okay
if ( !$user = $wp_xmlrpc_server->login($username, $password) ) {
return $wp_xmlrpc_server->error;
}
//$wp_query = new WP_Query("p=" . $post_id);
//if ($wp_query->have_posts())
//{
$custom_fields = get_post_meta($post_id);
return $custom_fields;
//}
return "not found";
}
function ssz_add_new_post_meta($args)
{
// Parse the arguments, assuming they're in the correct order
$blodID = $args[0];
$username = $args[1];
$password = $args[2];
$data = $args[3];
global $wp_xmlrpc_server;
// Let's run a check to see if credentials are okay
if ( !$user = $wp_xmlrpc_server->login($username, $password) ) {
return $wp_xmlrpc_server->error;
}
$post_id=$data["post_id"];
$custom_fields = $data["custom_fields"];
foreach ( $custom_fields as $meta_data ){
update_post_meta($post_id, $meta_data['key'], $meta_data['value']);
}
// Just output something ;)
return "Done!";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment