在自定义查询中保存重复选项值

时间:2019-08-27 作者:Tyler Robinson

我有一个带有帖子类型的自定义子菜单页面,我在其中查询所有帖子,并添加自定义文本区域和下拉列表,每个条目都会重复这些内容。每个字段的id和名称都是相同的。接受字段的结尾与文章id连字号。我希望能够保存在这些字段中输入的值,最好是在输入数据后保存,而不是单击保存按钮。

我认为需要将这些texatareas和下拉列表添加到一个组中以保存它们,但我以前没有这样做过。我通常在个人帖子中有这些价值观。但是,我需要将此部分与职位类型中的实际职位分开。

任何建议都会很有帮助。提前感谢!!!

这是我到目前为止为这个子菜单页所做的。

$args = array( \'post_type\' => \'properties\', \'posts_per_page\' => -1, \'orderby\' => \'title\', \'order\' => \'DESC\', \'post_status\' => \'publish\' );

$myposts = get_posts( $args );

?>
    <div class="wrap">
        <h1>Status:</h1>

        <style>
            .widefat .room-column {
                width: 3.2em;
                vertical-align: top;
            }
            .widefat textarea {
                width: 100%;
            }
        </style>
        <form action="" method="post">
            <input type="submit" name="update_statuses" value="Update" class="button-primary" />
            <table class="wp-list-table widefat fixed striped posts">
                <thead>
                    <tr>
                        <th class="manage-column column-cb room-column">Beds</th>
                        <th class="manage-column column-columnname">Address</th>
                        <th class="manage-column column-columnname">Renewal</th>
                        <th class="manage-column column-columnname">Future Rent</th>
                        <th class="manage-column column-columnname">Availability Date</th>
                        <th class="manage-column column-columnname">Deposit</th>
                        <th class="manage-column column-columnname">Last Showing</th>
                        <th class="manage-column column-columnname">Status</th>
                        <th class="manage-column column-columnname">Date</th>
                        <th class="manage-column column-columnname">Initials</th>
                        <th class="manage-column column-columnname">Notes</th>
                    </tr>
                </thead>
            <?php
            $count = 0;
            foreach( $myposts as $post ) : setup_postdata($post);
                $count++;
                $bedrooms = wp_get_post_terms($post->ID, \'bedrooms\', array("fields" => "all"));
                foreach( $bedrooms as $room ) { 
                    $bedSlug = $room->slug;
                }
                $bedStripped = preg_replace(\'/[^0-9]/\', \'\', $bedSlug);
                ?>
                <tr>
                    <th class="room-column">
                        <?php echo $bedStripped; ?>
                    </th>
                    <td>
                        <a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a>
                    </td>
                    <td>
                        <select name="renewal-<?php echo $post->ID; ?>" id="renewal-<?php echo $post->ID; ?>">
                            <option value="0">Choose</option>
                            <option value="No">No</option>
                            <option value="Yes">Yes</option>
                        </select>
                    </td>
                    <td>
                        <textarea name="future-rent-<?php echo $post->ID; ?>" id="future-rent-<?php echo $post->ID; ?>"></textarea>
                    </td>
                    <td>
                        <textarea name="available-<?php echo $post->ID; ?>" id="available-<?php echo $post->ID; ?>"></textarea>
                    </td>
                    <td>
                        <textarea name="deposit-<?php echo $post->ID; ?>" id="deposit-<?php echo $post->ID; ?>"></textarea>
                    </td>
                    <td>
                        <textarea name="last-showing-<?php echo $post->ID; ?>" id="last-showing-<?php echo $post->ID; ?>"></textarea>
                    </td>
                    <td>
                        <select name="status-<?php echo $post->ID; ?>" id="status-<?php echo $post->ID; ?>">
                            <option value="--">-</option>
                            <option value="nf">Needs Fees</option>
                            <option value="bgi">BGI</option>
                            <option value="fa">Final Approval</option>
                            <option value="a">Approved</option>
                            <option value="nd">Need Deposit</option>
                            <option value="dp">Deposit Paid</option>
                            <option value="ats">Appt to Sign</option>
                            <option value="rnl">Renew Lease</option>
                            <option value="ls">Lease Signed</option>
                        </select>
                    </td>
                    <td>
                        <textarea name="date-<?php echo $post->ID; ?>" id="date-<?php echo $post->ID; ?>"></textarea>
                    </td>
                    <td>
                        <textarea name="initials-<?php echo $post->ID; ?>" id="initials-<?php echo $post->ID; ?>"></textarea>
                    </td>
                    <td>
                        <textarea name="notes-<?php echo $post->ID; ?>" id="notes-<?php echo $post->ID; ?>"></textarea>
                    </td>
                </tr>
                <?php
            endforeach;
            echo \'<div style="float:right;">Total: \'.$count.\'</div>\';
            ?>
            </table>
            <input type="submit" name="update_statuses" value="Update" class="button-primary" />
        </form>

    </div>

2 个回复
最合适的回答,由SO网友:Antti Koskinen 整理而成

如果我没听错的话,你基本上是在问如何按帖子ID对输入进行分组?如果是这样的话,你可以这样做

// group by input
name="renewal[<?php echo $post->ID; ?>]"    
// when saving
$_POST[\'renewal\'] // array of input values with each having post_id as key

// group by post ID
name="data[<?php echo $post->ID; ?>][renewal/future-rent/etc.]"    
// when saving
$_POST[\'data\'] // array of arrays, each with post_id as key. post_id specific subarrays have input names as keys and input values as values

SO网友:juananruiz

在这个场景中,我更喜欢使用AJAX,在修改每个值时保存它,而不需要单击按钮。

您需要:

每行只打印一次帖子ID,一个JavaScript文件捕获您的更改并将其发送给admin ajax。php是一个动作挂钩,用于声明处理ajax帖子的函数,我为您制作了一个普通帖子的示例插件,只编辑一个字段

<?php
/**
* Plugin Name:  KFP Ajax Update
* Description:  Probando a actualizar campos con Ajax
* Version:      0.1.1
* Author:       Juanan Ruiz
* Author URI:   https://kungfupress.com/
*/

// Admin menu
add_action("admin_menu", "Kfp_Ajax_Update_menu");

// Links the hook to the function that handles the ajax post update
add_action(\'wp_ajax_kfp_ajax_update\', \'Kfp_Ajax_update\');

/**
 * Add the admin menu 
 *
 * @return void
 */
function Kfp_Ajax_Update_menu()
{
    add_menu_page("Formulario Ajax Update", "Ajax Update", "manage_options",
        "kfp_ajax_update_menu", "Kfp_Ajax_Update_admin", "dashicons-feedback", 45);
}

 /**
 * Create the admin page where you show and edit your data
 *
 * @return void
 */
 function Kfp_Ajax_Update_admin()
 {

     wp_enqueue_script(\'kfp-ajax-update\', plugins_url(\'/js/ajax-update.js\', __FILE__));
     wp_localize_script(\'kfp-ajax-update\', \'ajax_object\', array(
         \'ajax_url\' => admin_url(\'admin-ajax.php\'),
         \'ajax_nonce\' => wp_create_nonce(\'ajax_update_\' . admin_url(\'admin-ajax.php\')),
     ));
     $args = array( \'post_type\' => \'post\', \'posts_per_page\' => -1, \'orderby\' =>               
    \'title\', \'order\' => \'DESC\', \'post_status\' => \'publish\' );
    $myposts = get_posts( $args );

    $html = \'<div class="wrap"><h1>Ajax Update</h1>\';
    $html .= \'<table class="wp-list-table widefat fixed striped posts">\';
    $html .= \'<thead><tr>\';
    $html .= \'<th class="manage-column column-columnname"  width="30%">Title</th>\';
    $html .= \'<th class="manage-column column-columnname">Excerpt</th>\';
    $html .= \'</tr></thead><tbody>\';
    foreach($myposts as $post) {
        $html .= "<tr data-post_id=\'$post->ID\'>";
        $html .= "<td>$post->post_title</td>";
        $html .= "<td><textarea class=\'auto-update\' style=\'width:100%;\'>$post->post_excerpt</textarea></td>";
        $html .= "</tr>";
    }
    $html .= \'</tbody></table></div>\';
    echo $html;
}

/**
 * Handle ajax post
 *
 * @return void
 */
function Kfp_Ajax_update() 
    {
    if ( defined(\'DOING_AJAX\') && DOING_AJAX
        && wp_verify_nonce($_POST[\'nonce\'], \'ajax_update_\' . admin_url( \'admin-ajax.php\'))) {
        $args = array(
            \'ID\' => $_POST[\'post_id\'],
            \'post_excerpt\' => $_POST[\'post_excerpt\']
        );
        wp_update_post($args);
        echo "Ok";
        die();
    } else {
        die(\'Error\');
    }
}
以及javascript文件(js/ajax-update.js:

jQuery(document).ready(function($){
    $(\'.auto-update\').blur(function(event){
        $.post(ajax_object.ajax_url, 
            {
                action:\'kfp_ajax_update\', 
                post_id:$(this).parents(\'tr\').data(\'post_id\'),
                post_excerpt: $(this).val(),
                nonce: ajax_object.ajax_nonce
            }, 
            function(response) {
                return true;
            });
        return false;
    });
});
我输入了密码https://github.com/kungfupress/kfp-ajax-update

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register