I need to add a column to the WordPress posts table. The column will contain numeric values so that I can make very simple queries with rows ordered by that field. By now the values are saved as post meta but I need to have them in the post table so that I can order the posts by that field.
I know that I could just call the query on the post_meta table and order the rows by the value, then get the post through the post_ID. However, this way anytime I do a search, the posts with the same value wouldn't be ordered alphabetically but only by post_meta_ID.
So my question is: if I add a column to the wp_posts table, will it be removed/edited on the next WordPress update? There's a way to make it permanent or to update it with Wordpress?
答案 0 :(得分:3)
不,即使更新核心,您的自定义列也将保持不变。更新只会影响默认表和列,而不会影响自定义表。
要添加自定义列,您可以使用以下代码,只有在尚未存在的情况下才会添加该代码。
$myPosts = $wpdb->get_row("SELECT * FROM wp_posts");
//Add column if not present.
if(!isset($myPosts->my_custom_posts_column)){
$wpdb->query("ALTER TABLE wp_posts ADD my_custom_posts_column INT(1)");
}
(Source)