1

I have a custom query that has some repeat option values. I have three tabs that query Published, Draft and All. When under the Published tab and saving a value, it removes all the values under Drafts and vise versa. I need to be able to save the current values entered and keep the others as well. Below is my current code. Any suggestions?

<?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

}
?>
0

I'm having some dificulties in understaning what's going in your code. But I would suspect this problematic part is,

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);
}

You're just updating whatever there is on $_POST to the different options. There should probably be some kind of conditionals to check that you're getting the data you're expecting and not overwriting anything that isn't supposed to be overwritten.

To start debugging the code I would recommend you to add the following to the beginning of your page function.

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
}

With this whenever you click your form's submit button you get to see what actually gets sent to the server. So you would do,

  1. (Uncomment debugging if statement)
  2. Click submit to send the form
  3. Inspect the dumped form data
  4. Adjust your code
  5. Comment out the debugging if statement
  6. Refresh page and send the form again
  7. See what happened, rinse and repeat

As a side note, if the data you're saving is related to single posts, you could consider using custom taxonomies (e.g. for status, renewal, app-pending..) for the data instead of saving it as options. Or if it is unique to posts, e.g. dates or deposit sum, then save the data as post meta. This way you'd save post related data in more "correct" place. This would make querying for different post easier as taxonomies and post meta are supported in WP_Query.

Also to make the rendering more clear, you could consider breaking the long function into smaller ones. For example, script in js file, styles in css file, posts query into a separate function (which returns posts), saving in one function, html structure in separate view file, etc.. And for the select options you can use selected().

1
  • I ran the var_dump of $_POST and the only values being updated in the output were the ones from the Publish query while under the "Active" tab. There are 30 posts that are published and 30 values of each field array. The field saves under "Published". But if I switch over to "Draft" and save then the one on "Published" is gone and vise versa. Nov 28 '19 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.