[英]add a field to wordpress post
I am trying to add an extra field in my Add Post , i added a field to wp_post that can be 0 or 1 and then i wanna add check box into publish box in wordpress add post or edit post that checked when that field is 1 or unchecked when 0 and also it should can be save after submit update 我试图在我的添加帖子中添加一个额外的字段,我向wp_post添加了一个可以为0或1的字段,然后我想将复选框添加到wordpress添加帖子的发布框中,或编辑该字段为1或1时选中的帖子未选中时为0,也应该可以在提交更新后保存
I know that I can use Custom Field Templates, but the problem is that these custom fields insert the values into wp_postmeta and not wp_post, and I need everything for the single post in the same table. 我知道我可以使用“自定义字段模板”,但是问题是这些自定义字段将值插入wp_postmeta而不是wp_post,并且我需要同一表中的单个帖子的所有内容。
actually i need to add extra field for using a query that retrieve some record that i need for converting to json and the read it not all posts, for application on android and ios 实际上,我需要为使用查询的查询添加额外的字段,以检索一些我转换为json所需的记录,并读取它的内容不是全部,适用于android和ios
First of all, you shouldn't modify the core table, as a lot of people already mentioned. 首先,正如许多人已经提到的那样,您不应该修改核心表。 You can do a select using join from both tables like that: 您可以使用来自两个表的联接进行选择,如下所示:
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = 'tag'
AND $wpdb->postmeta.meta_value = 'email'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_date < NOW()
ORDER BY $wpdb->posts.post_date DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
More about it here: http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query 此处的更多信息: http : //codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query
Other than that, what exactly is your problem? 除此之外,您到底是什么问题? To display a custom checkbox you can use add_meta_box function. 要显示自定义复选框,可以使用add_meta_box函数。 To update it you need to add your function to wp_insert_post_data
hook and then insert your data to the database, something like that: 要对其进行更新,您需要将函数添加到wp_insert_post_data
挂钩,然后将数据插入数据库,如下所示:
add_filter('wp_insert_post_data', 'so27747402_save_data');
function so27747402_save_data($post_san){
global $wpdb;
global $post;
$checkbox = (isset($_POST['so27747402_checkbox']) && $_POST['so27747402_checkbox'] == 1);
update_post_meta($post->ID, '_so27747402_checkbox', $checkbox);
return $post_san;
}
Or like that if you insist on doing it via changes to the core table: 如果您坚持通过更改核心表来执行此操作,则可以这样:
add_filter('wp_insert_post_data', 'so27747402_save_data');
function so27747402_save_data($post_san){
global $wpdb;
global $post;
$checkbox = (isset($_POST['so27747402_checkbox']) && $_POST['so27747402_checkbox'] == 1);
$wpdb->query("UPDATE $wpdb->posts SET checkbox = $checkbox WHERE ID = $post->ID");
return $post_san;
}
But yeah, it's a pretty bad way. 但是,是的,这是一种非常糟糕的方法。 You should use update_post_meta
instead. 您应该改用update_post_meta
。
we have plugin for add custom filed to the post. 我们有用于将自定义字段添加到帖子的插件。
https://wordpress.org/plugins/advanced-custom-fields/ https://wordpress.org/plugins/advanced-custom-fields/
1.crete a field 1.创建一个字段
2.assign to post 2.分配发布
The best solve is Relwis's Meta Box plug-in for this. 最好的解决方法是Relwis的Meta Box插件。 You can use it. 您可以使用它。 Source link 源链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.