admin管理员组文章数量:1122832
I have some meta fields in WordPress created by theme and i want to update it by using php.
The metafield stores url and i want to add some string after current url in custom meta.
i tried these method Assign/update the custom field value for all posts
and made changes
'post_category' => array(14,16,19);
to select a particular category but it edited all posts of all categories.
and used this method to change meta field but it removed url and changet field.
$dealcode = "&deal_code=coupon";
$urlvalue = get_post_meta( $post->ID, 'metakey', false);
$urlvalue = $urlvalue . $dealcode;
update_post_meta( $post->ID, 'metakey', $urlvalue );
please can anyone fix this. Thanks.
I have some meta fields in WordPress created by theme and i want to update it by using php.
The metafield stores url and i want to add some string after current url in custom meta.
i tried these method Assign/update the custom field value for all posts
and made changes
'post_category' => array(14,16,19);
to select a particular category but it edited all posts of all categories.
and used this method to change meta field but it removed url and changet field.
$dealcode = "&deal_code=coupon";
$urlvalue = get_post_meta( $post->ID, 'metakey', false);
$urlvalue = $urlvalue . $dealcode;
update_post_meta( $post->ID, 'metakey', $urlvalue );
please can anyone fix this. Thanks.
Share Improve this question edited Jun 5, 2018 at 5:36 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Jun 5, 2018 at 5:11 MangalMangal 235 bronze badges 1 |1 Answer
Reset to default 0OK, so a little bit of guessing is involved in that answer, since you've posted only parts of your code, but... I assume that your code looks like that:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'post_category' => array(14,16,19)
);
$posts = get_posts($args);
foreach ( $posts as $post ) {
$dealcode = "&deal_code=coupon";
$urlvalue = get_post_meta( $post->ID, 'metakey', false);
$urlvalue = $urlvalue . $dealcode;
update_post_meta( $post->ID, 'metakey', $urlvalue );
}
If so, then the problem lies in this line:
'post_category' => array(14,16,19);
There is no post_category
argument for WP_Query...
The correct argument is category__in
, so your code should look like this:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'category__in' => array(14,16,19)
);
$posts = get_posts($args);
foreach ( $posts as $post ) {
$dealcode = "&deal_code=coupon";
$urlvalue = get_post_meta( $post->ID, 'metakey', false);
$urlvalue = $urlvalue . $dealcode;
update_post_meta( $post->ID, 'metakey', $urlvalue );
}
PS. Since you only use IDs, there is no point in getting whole posts. It would be much more efficient if you used fields => 'ids'
in your query.
本文标签: Update Post metafield of specific categories
版权声明:本文标题:Update Post metafield of specific categories 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736282575a1926684.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'post_category' => array(14,16,19);
– Krzysiek Dróżdż Commented Jun 5, 2018 at 6:47