WordPress-列出具有特定meta_key值的自定义类型


WordPress - List Custom Type With Specific meta_key value

所以我有以下

SELECT wp_posts.ID, wp_posts.post_author,wp_posts.post_date,wp_posts.post_content,wp_posts.post_title,wp_posts.post_excerpt,wp_postmeta.meta_value
FROM wp_posts
INNER JOIN wp_postmeta
ON wp_posts.id=wp_postmeta.post_id
WHERE wp_postmeta.meta_key='cm_video' and post_status='publish'
ORDER BY wp_posts.id DESC;

这几乎带回了我需要的东西。元值的形式为

a:2:{s:4:"code";s:27:"http://youtu.be/xpiL05fCE1E";s:4:"type";s:7:"youtube";}

恢复http://youtu.be/xpiL05fCE1E最简单的方法是什么?这里还有各种其他的值,比如embed和iframe代码。

WordPress内部有什么东西我应该使用吗?

我想出了这个办法,似乎奏效了。想法?

SELECT 
wp_posts.ID, wp_posts.post_author,wp_posts.post_date,wp_posts.post_content,wp_posts.post_title,wp_posts.post_excerpt,SUBSTRING_INDEX(SUBSTRING_INDEX(wp_postmeta.meta_value, '";', 2), ':"', -1)
FROM wp_posts
INNER JOIN wp_postmeta
ON wp_posts.id=wp_postmeta.post_id
WHERE wp_postmeta.meta_key='cm_video' and post_status='publish'

感谢

使用unserialize()方法

$a='a:2:{s:4:"code";s:27:"http://youtu.be/xpiL05fCE1E";s:4:"type";s:7:"youtube";}';
$a = unserialize($a);
print_r($a);
//output
Array ( [code] => http://youtu.be/xpiL05fCE1E [type] => youtube )
echo $a['code'];

以下是两种类型的可变

a = array and 2 is the dimension of the array
s = string and 4 is the length of the string

我们将通过wp_query:获得相同的正确格式

     $query = new WP_Query( array ( 
                                    'post_type' => 'product', 
                                    'orderby' => 'meta_value_num', 
                                    'meta_key' => 'cm_video' ) );
     print_r($query);

现在,如果$query对象中有代码元素和您的值。打印:

     echo $query->code;

希望它对你有用,如果你需要任何进一步的帮助,请告诉我。