如何从帖子元数据中提取数据

时间:2017-06-24 作者:BSZ

使用从数据库中提取信息的最佳方法是什么get_post_meta?

field name = invoice_details

value = a:20:{s:15:"price_per_night";s:3:"150";s:12:"no_of_nights";s:1:"1";s:23:"price_calulating_nights";s:3:"150";s:22:"price_after_commission";s:3:"150";s:13:"discount_type";s:0:"";s:20:"price_after_discount";s:3:"150";s:16:"discounted_price";s:1:"0";s:19:"discount_percentage";s:0:"";s:17:"total_service_fee";s:2:"60";s:11:"total_price";s:3:"210";s:8:"currency";s:3:"GBP";s:7:"post_id";s:2:"21";s:8:"check_in";s:10:"02-07-2017";s:9:"check_out";s:10:"03-07-2017";s:6:"guests";s:1:"1";s:16:"security_deposit";s:3:"100";s:10:"secret_key";s:30:"FfmtXmaPOKbrS8B1XLugufiD3Hdjxl";s:10:"updated_at";s:19:"2017-06-25 17:04:33";s:18:"booking_request_by";i:2;s:17:"step_2_attributes";a:9:{s:9:"firstname";s:3:"Tom";s:8:"lastname";s:5:"Smith";s:14:"street_address";s:3:"229";s:11:"postal_code";s:8:"YW11 3DU";s:4:"city";s:6:"London";s:7:"country";s:14:"United Kingdom";s:5:"phone";s:10:"0799999999";s:11:"guest_fname";a:1:{i:1;s:5:"Bella";}s:11:"guest_lname";a:1:{i:1;s:4:"King";}}}
例如,如何将“firstname”字段检索为“James”?

我目前正在使用:

$billing_firstname = get_post_meta( $post->ID, $invoice_details[\'firstname\'] , true );

任何帮助都将不胜感激。

非常感谢。

未序列化值:

array (
  \'price_per_night\' => \'150\',
  \'no_of_nights\' => \'1\',
  \'price_calulating_nights\' => \'150\',
  \'price_after_commission\' => \'150\',
  \'discount_type\' => \'\',
  \'price_after_discount\' => \'150\',
  \'discounted_price\' => \'0\',
  \'discount_percentage\' => \'\',
  \'total_service_fee\' => \'60\',
  \'total_price\' => \'210\',
  \'currency\' => \'GBP\',
  \'post_id\' => \'21\',
  \'check_in\' => \'02-07-2017\',
  \'check_out\' => \'03-07-2017\',
  \'guests\' => \'1\',
  \'security_deposit\' => \'100\',
  \'secret_key\' => \'FfmtXmaPOKbrS8B1XLugufiD3Hdjxl\',
  \'updated_at\' => \'2017-06-25 17:04:33\',
  \'booking_request_by\' => 2,
  \'step_2_attributes\' => 
  array (
    \'firstname\' => \'Tom\',
    \'lastname\' => \'Smith\',
    \'street_address\' => \'229\',
    \'postal_code\' => \'YW11 3DU\',
    \'city\' => \'London\',
    \'country\' => \'United Kingdom\',
    \'phone\' => \'0799999999\',
    \'guest_fname\' => 
    array (
      1 => \'Bella\',
    ),
    \'guest_lname\' => 
    array (
      1 => \'King\',
    ),
  ),
)

1 个回复
SO网友:Mayeenul Islam

我尝试了在线反序列化工具,但未能解释您的价值,因为它已损坏。

无论如何,获取值的方法如下所示:

// Get the value from db first
$meta_value = get_post_meta( $post->ID, \'invoice_details\' , true ); //\'invoice_details\' is a string, not a variable

// Get to the desired value
$desired_value = $meta_value[\'array_key\']; // value from first step of array
或:

$desired_value = $meta_value[\'array_key\'][\'array_key_second\']; // value from second step of array
你应该var_dump($meta_value) 在拥有$meta_value 了解如何处理数据。

编辑我只是按如下方式组织了您的阵列:

<?php
array (
    \'price_per_night\' => \'150\',
    \'no_of_nights\' => \'1\',
    \'price_calulating_nights\' => \'150\',
    \'price_after_commission\' => \'150\',
    \'discount_type\' => \'\',
    \'price_after_discount\' => \'150\',
    \'discounted_price\' => \'0\',
    \'discount_percentage\' => \'\',
    \'total_service_fee\' => \'60\',
    \'total_price\' => \'210\',
    \'currency\' => \'GBP\',
    \'post_id\' => \'21\',
    \'check_in\' => \'02-07-2017\',
    \'check_out\' => \'03-07-2017\',
    \'guests\' => \'1\',
    \'security_deposit\' => \'100\',
    \'secret_key\' => \'FfmtXmaPOKbrS8B1XLugufiD3Hdjxl\',
    \'updated_at\' => \'2017-06-25 17:04:33\',
    \'booking_request_by\' => 2,
    \'step_2_attributes\' => array (
        \'firstname\' => \'Tom\',
        \'lastname\' => \'Smith\',
        \'street_address\' => \'229\',
        \'postal_code\' => \'YW11 3DU\',
        \'city\' => \'London\',
        \'country\' => \'United Kingdom\',
        \'phone\' => \'0799999999\',
        \'guest_fname\' => array (
            1 => \'Bella\',
        ),
        \'guest_lname\' => array (
            1 => \'King\',
        ),
    ),
);
它本身给了你答案。是的,只需访问嵌套键:

$firstname = $meta_value[\'step_2_attributes\'][\'firstname\'];
如果\'firstname\' 设置后,您将获得名字。

客人名字:

$guestFirstname = $meta_value[\'step_2_attributes\'][\'guest_fname\'][1];

结束

相关推荐

列出分类法:如果分类法没有POST,就不要列出分类法--取决于定制的POST-META?

这可能很难解释,我不知道是否有解决办法!?我有一个名为“wr\\u event”的自定义帖子类型和一个名为“event\\u type”的分层自定义分类法。自定义帖子类型有一个元框,用于event_date 并且与此帖子类型关联的所有帖子都按以下方式排序event_date. 我在循环中有一个特殊的条件来查询event_date 已经发生了-在这种情况下,它没有显示,但只列在我的档案中。就像你可以使用wp_list_categories() 我编写了一个自定义函数,它以完全相同的方式列出所有分类术语。现在