为什么 get_post_meta 不起作用?

问题描述 投票:0回答:1

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

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

我能够看到 phpmyadmin 中存在的值。我在 action(save_post,sftoproject)plugin 中使用它,但它在我的代码中显示任何值。

这是我的代码

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;
}
php wordpress
1个回答
0
投票

来自 WP 文档

它是 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)
© www.soinside.com 2019 - 2024. All rights reserved.