我刚刚向特定的 post_type
添加了一个自定义字段。假设帖子类型是“书籍”。
我想为每个 post_id
和 post_type
图书插入数据库默认值 0。
基本上,我需要获取 post_id
,使用该 post_id
添加 post_id
、meta_key
和 meta_value
到 postmeta 表。
一些伪插入语句可能如下所示:
INSERT INTO postmeta WHERE (post_type = "Book" FROM other_table GET post_id) post_id = post_id, meta_key = newfield, meta_value = 0
另一个解决方案可能是改变我在 WordPress 中查询帖子的方式:
当前,当我使用 meta_key 参数进行查询时,只会提取填写了特定 meta_key 的帖子。有没有办法显示也没有此元 key 的帖子?我需要它来对具有 meta_key 的帖子进行排序。
编辑:
我在 wp 数据库中尝试了这个:
INSERT INTO wp_postmeta(`post_id`,`meta_key`,`meta_value`) VALUES((SELECT ID FROM wp_posts tb WHERE tb.post_type = 'support'),'category_order','0')
这基本上就是我想要做的,除了它应该允许多个值。目前还没有。
最佳答案
我过去必须做这样的工作,并且我坚持使用 WordPress 基础。在向帖子添加数据时我会避开 SQL。
基本上我做了一个页面模板,在里面我只是循环帖子并使用 update_post_meta,如下所示:
<?php
$args = array(
'post_type'=> 'book',
);
query_posts( $args );
// the Loop
while (have_posts()) : the_post();
//update the post meta, while looping
update_post_meta($post->ID, 'fieldname', 0);
endwhile;
?>
刚刚测试了代码,它可以工作。
关于php - 为每个 id 添加元值,其中 post_type = ??文字新闻,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26848230/