web-dev-qa-db-ja.com

カスタム投稿タイプのすべてのタグをIDで取得する方法

簡単な質問、カスタム投稿タイプのすべてのタグをIDで取得する方法post_type = product.

私は http://codex.wordpress.org/Function_Reference/wp_get_post_tags を試してみましたが、print_rで何も返されませんでした。

だから私はこれを試した、

$term_list = wp_get_post_terms($post->ID, 'product_tag', array("fields" => "all"));
print_r($term_list);

そして私のprint_r($term_list);にタグがついてきました。ありがとう

1
sohan

ループアプローチ:通常はarchive- {custom_post} .phpファイル。

最初

custom_post_plural特定の種類のカスタム投稿のグループを表します。

Custom_post_pluralの例: products

custom_post_singular個々のカスタム投稿タイプを表します。

Custom_post_singularの例: product

_秒_

var $ args_custom_post_plural はWP_Queryのパラメータです。

var $ custom_post_plural はクエリの実行です。

私はvar $ custom_post_plural_output を使用してWP_Objectの内容を、特にpostsという用語で繰り返し、その内容を「配列にやさしい」ようにしました。

ご覧のとおり、入れ子になった反復処理にAhmad命令を部分的に使用しました。

$args_custom_post_plural=array(
   'post_type' => 'custom_post_singular',
   'post_status' => 'publish', 
   'posts_per_page' => -1, 
   'fields' => 'ids', 
   'order_by' =>'id', 
   'order' => 'ASC'
);
$custom_post_plural = new WP_Query($args_custom_post_plural);
$custom_post_plural_output = $custom_post_plural->posts;
for ($i=0; $i < count($custom_post_plural_output); $i++) { 
   $tags = wp_get_post_tags($custom_post_plural_output[$i]);
   $buffer_tags ='';
   foreach ( $tags as $tag ) {
      $buffer_tags .= $tag->name . ',';
   }
}
echo $buffer_tags;

最後に

参考単一の{custom_post} .phpファイルでこれを使用したい場合は、次のコードを使用できます。

$tags = wp_get_post_tags($post->ID);
foreach ( $tags as $tag ) {
   $buffer_tags .= $tag->name . ',';
}
echo $buffer_tags;

何かを表示するにはリンクされた投稿が必要です。

ハッピーコーディング.

PS。 @cjbjどうして私の編集を地獄で消してしまったのですか。ここでのひどい管理、そして私は評判ポイントの量のために私がコメントに答えることができないのでとても悪意があります。

1
luis

投稿IDでタグを取得する必要がある場合は、次の関数を使用できます。メソッドはデータベースクエリに基づいているため、これはどこでも機能します。

function sc_tf_get_tags_as_array($post_id){
        global $wpdb;
        $tbl_terms = $wpdb->prefix . "terms";
        $tbl_term_relationships = $wpdb->prefix . "term_relationships";

        $sql = "SELECT name FROM $tbl_terms WHERE term_id in (SELECT term_taxonomy_id FROM $tbl_term_relationships WHERE object_id='$post_id');";
        $results = $wpdb->get_results($sql);

        if($results){
            foreach($results as $row){
                $tags_list[] = $row->name;
            }
        }

        return $tags_list;
    }
1
chandima

wp_get_post_tagsは投稿に対してのみ機能し、他の種類の投稿には機能しません。 /wp-includes/post.phpを見ると、$ taxonomyを 'post_tag'に設定してwp_get_post_terms関数を呼び出していることがわかります。

function wp_get_post_tags( $post_id = 0, $args = array() ) {
    return wp_get_post_terms( $post_id, 'post_tag', $args );
}

商品タグやその他の分類法では、代わりに get_the_terms() を使用できます。

$tags = get_the_terms( $prod_id, 'product_tag' );
$tags_names = array();
if ( ! empty( $tags ) ) {
    foreach ( $tags as $tag ) {
        $tags_names[] = $tag->name;
    }
}
0
David Najman