admin管理员组文章数量:1279117
I have the following code below, which loops through all posts in a db of books. I would like to tag book entries which do not have the tag $tag="book_".$booknum."_".$chapnum;
My output in the debug, shows that the query correctly identifies what I want.
BUT the line: $result= wp_add_post_tags((int) $id, $tag ); does not seem to be adding the tag to the post with the $id. I tried adding //wp_update_post( $post ); // wp_reset_postdata(); but both failed also.
while ($q->have_posts()) {
$post= $q->the_post();
$link = get_permalink();
$title=get_the_title();
$id=get_the_ID();
$content=get_the_content();
$booknum=$this->GetKeyValFromText($content, "book_num");
$chapnum=$this->GetKeyValFromText($content, "chap_num");
$tag="book_".$booknum."_".$chapnum;
$hasTag=has_tag($tag);
$ln=$this->MakeLink($title, $link);
if (!$hasTag) {
$this->debug("adding post tag to $ln ; $tag hasTAG=$hasTag , id=$id");
// wp_insert_term( 'post_tag', $tag );
$result= wp_add_post_tags((int) $id, $tag );
var_dump($result);
//wp_update_post( $post );
// wp_reset_postdata();
return;
}
}
I have the following code below, which loops through all posts in a db of books. I would like to tag book entries which do not have the tag $tag="book_".$booknum."_".$chapnum;
My output in the debug, shows that the query correctly identifies what I want.
BUT the line: $result= wp_add_post_tags((int) $id, $tag ); does not seem to be adding the tag to the post with the $id. I tried adding //wp_update_post( $post ); // wp_reset_postdata(); but both failed also.
while ($q->have_posts()) {
$post= $q->the_post();
$link = get_permalink();
$title=get_the_title();
$id=get_the_ID();
$content=get_the_content();
$booknum=$this->GetKeyValFromText($content, "book_num");
$chapnum=$this->GetKeyValFromText($content, "chap_num");
$tag="book_".$booknum."_".$chapnum;
$hasTag=has_tag($tag);
$ln=$this->MakeLink($title, $link);
if (!$hasTag) {
$this->debug("adding post tag to $ln ; $tag hasTAG=$hasTag , id=$id");
// wp_insert_term( 'post_tag', $tag );
$result= wp_add_post_tags((int) $id, $tag );
var_dump($result);
//wp_update_post( $post );
// wp_reset_postdata();
return;
}
}
Share
Improve this question
asked Nov 5, 2021 at 9:42
shelbypereirashelbypereira
1375 bronze badges
1 Answer
Reset to default 0I found the issue was with the polylang plugin. For the above to work I needed to add the language info and in particular associated the tag with the language of the post.
$result=wp_insert_term($tag, 'post_tag'); pll_set_term_language($result["term_id"], 'fr');
pll_set_term_language is the function provided by the polylang plugin for associating the newly added tag with the language (fr=French in the above example).
本文标签: queryhow to add tags to post in the Loop
版权声明:本文标题:query - how to add tags to post in the Loop? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741233294a2362509.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论