Sql 检索WordPress自定义文章类型分类法和相关术语
我想检索自定义帖子类型中使用的数据字段,检索分类法、类别、标记等数据。 我只熟悉检索在其中使用此选项的自定义字段。分类法、标记和类别的等效值是多少Sql 检索WordPress自定义文章类型分类法和相关术语,sql,wordpress,custom-post-type,taxonomy,custom-taxonomy,Sql,Wordpress,Custom Post Type,Taxonomy,Custom Taxonomy,我想检索自定义帖子类型中使用的数据字段,检索分类法、类别、标记等数据。 我只熟悉检索在其中使用此选项的自定义字段。分类法、标记和类别的等效值是多少 global $post, $wpdb; echo '<pre>'; print_r( get_post_custom($post->ID) ); echo '</pre>'; global$post,$wpdb; 回声'; 打印(获取发布自定义($post->ID)); 回声'; 在下面的代码中,您将获得当前$po
global $post, $wpdb;
echo '<pre>';
print_r( get_post_custom($post->ID) );
echo '</pre>';
global$post,$wpdb;
回声';
打印(获取发布自定义($post->ID));
回声';
在下面的代码中,您将获得当前$post
对象的所有分类法和自定义分类法
注意:要获取分类法(或自定义分类法),我没有使用WordPressget\u object\u taxonomies()
函数,因为它将获取post类型的所有分类法,而不是$post
本身的分类法。相反,我使用了一个非常轻量级的SQL查询,它将只获取为当前$post
设置的分类
此代码将显示分类标签名称,其中包含分隔的术语,仅当它不为空时
global$post,$wpdb;
$taxonomies=$wpdb->get_col(“选择DISTINCT tt.taxonomy
从{$wpdb->prefix}term_分类为tt
在tt.term\u taxonomy\u id=tr.term\u taxonomy\u id上将{$wpdb->prefix}term\u关系连接为tr
其中tr.object_id={$post->id});
//循环浏览此WP_Post对象的所有分类法
foreach($taxonomy作为$taxonomy){
//获取此分类法和此帖子ID的WP_术语对象
$terms=wp\u get\u post\u terms($post->ID,$taxonomy);
//如果此分类法具有此帖子ID的术语
如果(计数($terms)>0){
$term_names=array();
//获取分类标签名称
$taxonomy\u label\u name=get\u taxonomy($taxonomy)->labels->name;
$taxonomy\u label\u singular\u name=get\u taxonomy($taxonomy)->labels->singular\u name;
//循环遍历此分类法和此帖子ID的每个WP_术语对象
foreach($terms作为$term){
$term\u id=$term->term\u id;//术语id
$term\u slug=$term->slug;//术语slug
$term\u name=$term->name;//术语名称
$term\u parent=$term->parent;//术语父ID
$description=$term->description;//术语描述
$term\u link=get\u term\u link($term$taxonomy);//术语链接
//在数组中设置术语名称(用于测试显示)
$term\u name[]=$term\u name;
}
//输出分类标签名称,并用逗号分隔
回显“。$taxonomy\u label\u name.”:”。内爆(“,”,$term\u name)。“”;
}
}
测试和工作
global $post, $wpdb;
$taxonomies = $wpdb->get_col( "SELECT DISTINCT tt.taxonomy
FROM {$wpdb->prefix}term_taxonomy as tt
JOIN {$wpdb->prefix}term_relationships as tr ON tt.term_taxonomy_id = tr.term_taxonomy_id
WHERE tr.object_id = {$post->ID}" );
// Loop through all taxonomies for this WP_Post object
foreach ( $taxonomies as $taxonomy ) {
// Get the WP_Term objects for this taxomnomy and this post ID
$terms = wp_get_post_terms( $post->ID, $taxonomy );
// If this taxonomy has terms for this post ID
if( count($terms) > 0 ){
$term_names = array();
// Get the taxonomy label name
$taxonomy_label_name = get_taxonomy( $taxonomy )->labels->name;
$taxonomy_label_singular_name = get_taxonomy( $taxonomy )->labels->singular_name;
// Loop through each WP_Term object for this taxomnomy and this post ID
foreach ( $terms as $term ) {
$term_id = $term->term_id; // The term ID
$term_slug = $term->slug; // The term slug
$term_name = $term->name; // The term name
$term_parent = $term->parent; // The term parent ID
$description = $term->description; // The term description
$term_link = get_term_link( $term, $taxonomy ); // The term link
// Set the term name in an array (for testing display)
$term_names[] = $term_name;
}
// Output taxonomy label name with the coma separated terms
echo '<p><strong>' . $taxonomy_label_name . ':</strong> ' . implode(', ', $term_names) . '</p>';
}
}