为什么get_post_meta不起作用?

时间:2014-01-21 11:11:03

标签: wordpress wordpress-plugin

我正在使用以下get_post_meta函数,但它没有显示任何值,它显示空白结果

$meta_values = get_post_meta( $post_id, 'salary', true );

我能够看到phpmyadmin中存在的值。我在动作(save_post,sftoproject)中的插件中使用它,但是它显示了任何值我的代码。

这是我的代码

add_action('save_post','user_sf_project');
function user_sf_project($post_id){
$SOAP_CLIENT_BASEDIR = plugin_dir_path(__FILE__).'Force.com-Toolkit-for-PHP/soapclient/';
require_once ($SOAP_CLIENT_BASEDIR.'/SforcePartnerClient.php');
require_once ($SOAP_CLIENT_BASEDIR.'/SforceHeaderOptions.php');
require_once ('Force.com-Toolkit-for-PHP/Samples/userAuth.php');

try {
  $mySforceConnection = new SforcePartnerClient();
  $mySoapClient =  $mySforceConnection->createConnection($SOAP_CLIENT_BASEDIR.'/partner.wsdl.xml');
  $mylogin = $mySforceConnection->login("username", "password");

  $postdata = get_post($post_id);
  $meta_values = get_post_meta( $post_id, 'salary', true );
  $fields = array (
    'Name' => $post_id,
    'First_Name__c' => $postdata->post_title,
    'Salary__c' => $meta_values,
  );

  $sObject = new SObject();
  $sObject->fields = $fields;
  $sObject->type = 'NewObject__c';

  $createResponse = $mySforceConnection->create(array($sObject));

  print_r($createResponse);

  $ids = array();
  foreach ($createResponse as $createResult) {
    print_r($createResult);
    array_push($ids, $createResult->id);
  }

} catch (Exception $e) {
echo $mySforceConnection->getLastRequest();
echo $e->faultstring;
}

1 个答案:

答案 0 :(得分:0)

来自WP docs

  

它是get_metadata('post')的包装器。要返回所有自定义   字段,请参阅get_post_custom()。

您必须对自定义帖子类型使用get_post_custom()函数(不是get_post_meta),但我强烈建议您始终使用 get_metadata 而不是get_post_meta()和get_post_custom()来避免简单的问题...

get_metadata()的示例:

get_metadata('your_custom_post_type', $post_id, 'salary', true)