简体   繁体   中英

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

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.

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

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

Other than that, what exactly is your problem? To display a custom checkbox you can use add_meta_box function. 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:

    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.

we have plugin for add custom filed to the post.

https://wordpress.org/plugins/advanced-custom-fields/

1.crete a field

2.assign to post

The best solve is Relwis's Meta Box plug-in for this. You can use it. Source link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM