在已发布、暂存、全部查询时保存重复选项值

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

我有一个自定义查询,其中包含一些重复选项值。我有三个选项卡可以查询发布、草稿和全部。在“已发布”选项卡下保存值时,将删除“草稿”下的所有值,反之亦然。我需要能够保存当前输入的值,并保留其他值。下面是我当前的代码。有什么建议吗?

<?php
add_action(\'admin_menu\', \'properties_status_menu\'); 
function properties_status_menu() { 
    add_submenu_page( \'edit.php?post_type=properties\', \'Status\', \'Status\', \'manage_options\', \'properties-status\', \'properties_status_page_handler\');
}

function properties_status_page_handler(){
        global $post, $current, $searchOwner;

        function property_status_tabs( $current = \'active\') {

            $tabs = array( \'active\' => \'Active\', \'inactive\' => \'Inactive\', \'all\' => \'All\' );
            //echo \'<div id="icon-themes" class="icon32"><br></div>\';
            echo \'<h2 class="nav-tab-wrapper">\';
            foreach( $tabs as $tab => $name ){
                $class = ( $tab == $current ) ? \'nav-tab-active\' : \'\';
                echo \'<a class="nav-tab \' . $class . \'" href="/wp-admin/edit.php?post_type=properties&page=properties-status&tab=\' . $tab . \'">\' . $name . \'</a>\';

            }
            echo \'</h2>\';

        }

        $tab = ( ! empty( $_GET[\'tab\'] ) ) ? esc_attr( $_GET[\'tab\'] ) : \'active\';
        property_status_tabs( $tab );
        if(isset($_POST[\'searchProp\'])){
            $propTitle = $_POST[\'searchProp\'];
        }else {
            $propTitle = \'\';
        }
        if(isset($_POST[\'searchOwner\'])){
            $searchOwner = $_POST[\'searchOwner\'];
        }else {
            $searchOwner = \'\';
        }
        remove_all_filters(\'posts_orderby\');
        if($tab == \'active\' && isset($_POST[\'searchOwner\'])){
            $args = array(
                \'post_type\' => \'properties\',
                \'posts_per_page\' => -1,
                \'order\' => \'DESC\',
                \'orderby\' => \'title\',
                \'post_status\' => \'publish\',
                \'s\' => $propTitle,
                \'tax_query\' => array( // NOTE: array of arrays!
                    array(
                        \'taxonomy\' => \'owners\',
                        \'field\'    => \'slug\',
                        \'terms\'    => $searchOwner,
                        \'operartor\'    => \'IN\'
                    )
                )

            );
        }else if($tab == \'active\'){
            $args = array(
                \'post_type\' => \'properties\',
                \'posts_per_page\' => -1,
                \'orderby\' => \'title\',
                \'order\' => \'DESC\',
                \'post_status\' => \'publish\',
                \'s\' => $propTitle,
            );
        }
        //Inactive search by address
        if($tab == \'inactive\' && isset($_POST[\'searchProp\'])){
            $args = array(
                \'post_type\' => \'properties\',
                \'posts_per_page\' => -1,
                \'orderby\' => \'title\',
                \'order\' => \'DESC\',
                \'post_status\' => \'draft\',
                \'s\' => $propTitle,

            );
        }
        if($tab == \'inactive\' && isset($_POST[\'searchOwner\'])){
            $args = array(
                \'post_type\' => \'properties\',
                \'posts_per_page\' => -1,
                \'orderby\' => \'title\',
                \'order\' => \'DESC\',
                \'post_status\' => \'draft\',
                \'s\' => $propTitle,
                \'tax_query\' => array( // NOTE: array of arrays!
                    array(
                        \'taxonomy\' => \'owners\',
                        \'field\'    => \'slug\',
                        \'terms\'    => $searchOwner,
                        \'operartor\'    => \'IN\'
                    )
                )
            );
        }else if($tab == \'inactive\'){
            $args = array(
                \'post_type\' => \'properties\',
                \'posts_per_page\' => -1,
                \'orderby\' => \'title\',
                \'order\' => \'DESC\',
                \'post_status\' => \'draft\',
                \'s\' => $propTitle
            );
        }
        if($tab == \'all\' && isset($_POST[\'searchOwner\'])){
            $args = array(
                \'post_type\' => \'properties\',
                \'posts_per_page\' => -1,
                \'orderby\' => \'title\',
                \'order\' => \'DESC\',
                \'post_status\' => array(
                    \'publish\', \'draft\'
                ),
                \'s\' => $propTitle,

            );
        }else if($tab == \'all\'){
            $args = array(
                \'post_type\' => \'properties\',
                \'posts_per_page\' => -1,
                \'orderby\' => \'title\',
                \'order\' => \'DESC\',
                \'post_status\' => array(
                    \'publish\', \'draft\'
                ),
                \'s\' => $propTitle );
        }


        if(isset( $_POST[\'p_status_update\'] ) ){
            $renewal = $_POST[\'renewal\'];
            $frent = $_POST[\'future-rent\'];
            $available = $_POST[\'available\'];
            $deposit = $_POST[\'deposit\'];
            $lastShowing = $_POST[\'last-showing\'];
            $status = $_POST[\'status\'];
            $date = $_POST[\'date\'];
            $initials = $_POST[\'initials\'];
            $notes = $_POST[\'notes\'];


            update_option(\'renewal\', $renewal);
            update_option(\'future-rent\', $frent);
            update_option(\'available\', $available);
            update_option(\'deposit\', $deposit);
            update_option(\'last-showing\', $lastShowing);
            update_option(\'status\', $status);
            update_option(\'date\', $date);
            update_option(\'initials\', $initials);
            update_option(\'notes\', $notes);

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

        <style>
            .widefat .room-column {
                width: 3.2em;
                vertical-align: top;
            }
            .widefat textarea {
                width: 100%;
            }
            .button-primary {
                margin: 10px 0 10px 0 !important;
            }
            .widefat input {
                width: 95%;
            }
            input:disabled {
                background: #FFF !important;
                color: #000  !important;
            }
        </style>
        <script>
            jQuery(document).ready(function($){
                $(\'select[name="searchOwner"]\').change(function(){
                    $(\'#searchOwners\').submit();
                });
            });
        </script>
        <form id="searchProperties" name="searchProperties" method="post" action="" style="margin: 15px 15px 15px 0; float: left;">
            <label>Search By Street Address</label><div><input id="searchProp" name="searchProp" type="input" size="50" placeholder="Street address" /></div>
        </form>
        <form id="searchOwners" name="searchOwners" method="post" action="" style="margin: 15px 0 15px 0; float: left;">
            <label>Search By Owner</label> <?php fstudio_custom_taxonomy_dropdown( \'owners\', \'date\', \'DESC\', \'\', \'searchOwner\', \'select owner\' ); ?>
        </form>
        <div style="clear: both;"></div>
        <form action="" method="post">
             <?php wp_nonce_field( \'property_status_update\', \'p_status_update\' ); ?>
            <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
            //global $post;
            $count = 0;
            $propQuery = new WP_Query($args);
            //var_dump($propQuery->request);
            while ( $propQuery->have_posts() ) : $propQuery->the_post();
                $PID = $post->ID;
                $count++;
                $bedrooms = wp_get_post_terms($PID, \'bedrooms\', array("fields" => "all"));
                foreach( $bedrooms as $room ) { 
                    $bedSlug = $room->slug;
                }
                if ($bedSlug != "individual-lease") {
                    $bedStripped = preg_replace(\'/[^0-9]/\', \'\', $bedSlug);
                }else{
                    $bedStripped = \'1\';
                }
                $unitrent = wp_get_post_terms($post->ID, \'rent\', array("fields" => "all"));
                foreach( $unitrent as $rent ) { 
                    $rentprice = $rent->name;
                    $rentpriceSlug = $rent->slug;
                }
                $dateAvailable = get_cfc_field(\'propertysettings\', \'date-available\');
                ?>
                <tr>
                    <th class="room-column">
                        <?php echo $bedStripped; ?>
                    </th>
                    <td>
                        <a href="<?php echo get_edit_post_link(); ?>" target="_blank"><?php the_title(); ?></a>
                    </td>
                    <td>
                        <select name="renewal[<?php echo $PID; ?>]" id="renewal[<?php echo $PID; ?>]">
                            <option value="0" <?php if (get_option(\'renewal\')[$PID] == 0 ) echo \'selected\' ; ?>>Choose</option>
                            <option value="No" <?php if (get_option(\'renewal\')[$PID] == \'No\' ) echo \'selected\' ; ?>>No</option>
                            <option value="Yes" <?php if (get_option(\'renewal\')[$PID] == \'Yes\' ) echo \'selected\' ; ?>>Yes</option>
                        </select>
                    </td>
                    <td>
                        <!--<textarea name="future-rent[<?php //echo $PID; ?>]" id="future-rent[<?php //echo $PID; ?>]"><?php //echo get_option(\'future-rent\')[$PID]; ?></textarea>-->
                        <input type="text" disabled value="<?php echo $rentprice; ?>" />
                    </td>
                    <td>
                        <!--<textarea name="available[<?php //echo $PID; ?>]" id="available[<?php //echo $PID; ?>]"><?php //echo get_option(\'available\')[$PID]; ?></textarea>-->
                        <input type="text" disabled value="<?php echo $dateAvailable; ?>" />
                    </td>
                    <td>
                        <!--<textarea name="deposit[<?php //echo $PID; ?>]" id="deposit[<?php //echo $PID; ?>]"><?php //echo get_option(\'deposit\')[$PID]; ?></textarea>-->
                        <input type="text" disabled value="<?php the_cfc_field(\'leasingsettings\', \'deposit\'); ?>" />
                    </td>
                    <td>
                        <textarea name="last-showing[<?php echo $PID; ?>]" id="last-showing[<?php echo $PID; ?>]"><?php echo get_option(\'last-showing\')[$PID]; ?></textarea>
                    </td>
                    <td>
                        <select name="status[<?php echo $PID; ?>]" id="status[<?php echo $PID; ?>]">
                            <option value="0" <?php if (get_option(\'status\')[$PID] == 0 ) echo \'selected\' ; ?>>-</option>
                            <option value="nf" <?php if (get_option(\'status\')[$PID] == \'nf\' ) echo \'selected\' ; ?>>Needs Fees</option>
                            <option value="bgi" <?php if (get_option(\'status\')[$PID] == \'bgi\' ) echo \'selected\' ; ?>>BGI</option>
                            <option value="fa" <?php if (get_option(\'status\')[$PID] == \'fa\' ) echo \'selected\' ; ?>>Final Approval</option>
                            <option value="a" <?php if (get_option(\'status\')[$PID] == \'a\' ) echo \'selected\' ; ?>>Approved</option>
                            <option value="nd" <?php if (get_option(\'status\')[$PID] == \'nd\' ) echo \'selected\' ; ?>>Need Deposit</option>
                            <option value="dp" <?php if (get_option(\'status\')[$PID] == \'dp\' ) echo \'selected\' ; ?>>Deposit Paid</option>
                            <option value="ats" <?php if (get_option(\'status\')[$PID] == \'ats\' ) echo \'selected\' ; ?>>Appt to Sign</option>
                            <option value="rnl" <?php if (get_option(\'status\')[$PID] == \'rnl\' ) echo \'selected\' ; ?>>Renew Lease</option>
                            <option value="ls" <?php if (get_option(\'status\')[$PID] == \'ls\' ) echo \'selected\' ; ?>>Lease Signed</option>
                        </select>
                        <?php
                        if(isset( $_POST[\'p_status_update\'] ) ){
                            if (get_option(\'status\')[$PID] === \'bgi\' || get_option(\'status\')[$PID] === \'a\' || get_option(\'status\')[$PID] === \'nd\'){
                                update_post_meta( $PID, \'app-pending\', \'Yes\' );
                            }else if (get_option(\'status\')[$PID] != \'bgi\' || get_option(\'status\')[$PID] != \'a\' || get_option(\'status\')[$PID] != \'nd\'){
                                update_post_meta( $PID, \'app-pending\', \'No\' );
                            }

                            if (get_option(\'status\')[$PID] === \'dp\' && get_post_status ( $PID ) == \'publish\'){
                                wp_update_post(array(\'ID\' => $PID, \'post_status\' => \'draft\'));
                            }
                        }
                        ?>
                    </td>
                    <td>
                        <textarea name="date[<?php echo $PID; ?>]" id="date[<?php echo $PID; ?>]"><?php echo get_option(\'date\')[$PID]; ?></textarea>
                    </td>
                    <td>
                        <textarea name="initials[<?php echo $PID; ?>]" id="initials[<?php echo $PID; ?>]"><?php echo get_option(\'initials\')[$PID]; ?></textarea>
                    </td>
                    <td>
                        <textarea name="notes[<?php echo $PID; ?>]" id="notes[<?php echo $PID; ?>]"><?php echo get_option(\'notes\')[$PID]; ?></textarea>
                    </td>
                </tr>
                <?php
            endwhile; wp_reset_postdata();
            echo \'<div style="float:right;">Total: \'.$count.\'</div>\';
            ?>
            </table>
            <input type="submit" id="update_statuses" name="update_statuses" value="Update" class="button-primary" />
        </form>
    </div>
    <?php

}
?>

1 个回复
SO网友:Antti Koskinen

我在理解代码中的内容时遇到了一些困难。但我怀疑这个有问题的部分是,

if(isset( $_POST[\'p_status_update\'] ) ){
  $renewal = $_POST[\'renewal\'];
  $frent = $_POST[\'future-rent\'];
  $available = $_POST[\'available\'];
  $deposit = $_POST[\'deposit\'];
  $lastShowing = $_POST[\'last-showing\'];
  $status = $_POST[\'status\'];
  $date = $_POST[\'date\'];
  $initials = $_POST[\'initials\'];
  $notes = $_POST[\'notes\'];    

  update_option(\'renewal\', $renewal);
  update_option(\'future-rent\', $frent);
  update_option(\'available\', $available);
  update_option(\'deposit\', $deposit);
  update_option(\'last-showing\', $lastShowing);
  update_option(\'status\', $status);
  update_option(\'date\', $date);
  update_option(\'initials\', $initials);
  update_option(\'notes\', $notes);
}
你只是在更新电视上的内容$_POST 到不同的选项。可能应该有一些条件来检查您是否获得了预期的数据,并且没有覆盖任何不应该被覆盖的内容。

要开始调试代码,我建议您在页面函数的开头添加以下内容。

function properties_status_page_handler(){

  if ( isset($_POST[\'p_status_update\']) ) {
    // dump what is sent to the server
    var_dump($_POST);
    // log for later review
    error_log(print_r($_POST, true));
    // stop the code from executing, results in white screen with the $_POST dump showing
    die;    
  }

  // rest of the code
}
使用此功能,无论何时单击表单的提交按钮,都可以看到实际发送到服务器的内容。所以你会这么做,

如果要保存的数据与单篇文章相关,请单击“提交”发送表单,检查转储的表单数据,调整代码,注释掉调试if语句,然后再次发送表单,您可以考虑使用自定义分类法(例如,状态、续订、应用程序挂起…)而不是将其保存为选项。或者,如果它是帖子独有的,例如日期或存款总额,则将数据另存为帖子元。这样,您可以将帖子相关数据保存在更“正确”的位置。这将使查询不同的帖子更加容易,因为WP\\U查询中支持分类法和帖子元

此外,为了使渲染更加清晰,可以考虑将长函数分解为较小的函数。例如,js文件中的脚本、css文件中的样式、将查询发布到单独的函数(返回帖子)、保存在一个函数中、将html结构保存在单独的视图文件中等等。。对于您可以使用的选择选项selected().