• techaddict

    (@techaddict)


    How to create category when posting new post via XML RPC if the category doesn’t exist ?

    Currently i am using below function :

    function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$category,$keywords='',$encoding='UTF-8') {
        $title = htmlentities($title,ENT_NOQUOTES,$encoding);
        $keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);
    
        $content = array(
            'title'=>$title,
            'description'=>$body,
            'mt_allow_comments'=>1,  // 1 to allow comments
            'mt_allow_pings'=>0,  // 1 to allow trackbacks
            'post_type'=>'post',
            'mt_keywords'=>$keywords,
            'categories'=>array($category)
        );
        $params = array(0,$username,$password,$content,true);
        $request = xmlrpc_encode_request('metaWeblog.newPost',$params);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
        curl_setopt($ch, CURLOPT_URL, $rpcurl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 1);
        $results = curl_exec($ch);
        curl_close($ch);
        return $results;
    }

    But problem with it is that, it post the post with category only if it already exist, it doesn’t create new category.

Viewing 1 replies (of 1 total)
  • I’m using wp.newPost instead of metaWeblog.newPost and non-existing categories and tags are created automatically in the remote site.

    For example:

    $categories = wp_get_post_categories($id);
    	$cats = array();
    	foreach($categories as $c){
    		$cat = get_category( $c );
    		$cats[] = utf8_decode($cat->name);
    	}
    
    	$tags = get_the_tags($id);
    	$ts = array();
    	if ($tags) {
    		foreach($tags as $tag) {
    			$ts[] = utf8_decode($tag->name);
    		}
    	}
    
    [...]
    $content = array(
            'post_title'   => utf8_decode($p->post_title),
            'post_content' => utf8_decode($p->post_content),
    [...]
    	'terms_names' => array('category' => $cats, 'post_tag' => $ts ),
            'post_status' => 'pending',
    	);

Viewing 1 replies (of 1 total)
  • The topic ‘How to create category when posting new post via XML RPC ?’ is closed to new replies.