0

So I created a theme for my company to use and was running it locally for quite some time, but recently moved to a live server and, as expected, some issues arose. Most of them pretty trivial and easy to fix, but the biggest one has been, least surprisingly, the wp_list_pages function.

For some reason, it is listing doubles of each entry in every situation.

<?php echo wp_list_pages( 
      array(
        'title_li' => '<a href="members">Member Clients</a>',
        'meta_key' => 'member-checkbox', 
        'meta_value' => 'yes',));
    ?>
    <?php echo wp_list_pages( array(
        'title_li' => '<a href="phone-clients">Phone Clients</a>',
        'meta_key' => 'phone-checkbox',
        'meta_value' => 'yes',));
    ?>

The above is my list code that, as I said, was working correctly previously.

Below is my checkbox function code. This was also working perfectly.

add_action( 'save_post', 'member_page_meta_save' );

function member_page_circles_meta() {
    add_meta_box( 'member_circles_meta', __( 'Select Member Options', 'member_circles_textdomain' ), 'member_page_circles_meta_callback', 'page', 'side', 'low' );
}
add_action( 'add_meta_boxes', 'member_page_circles_meta' );

/**
 * Outputs the content of the meta box
 */

function member_page_circles_meta_callback( $post ) {
    $values2 = get_post_custom( $post->ID );
    $check2 = isset( $values2['member_circle_box_check'] ) ? esc_attr( $values['member_circle_box_check'][0] ) : '';
    wp_nonce_field( basename( __FILE__ ), 'member_page_circles_nonce' );
    $member_page_circles_stored_meta = get_post_meta( $post->ID );
    ?>

 <p>
    <div class="member_page_circles-row-content">
        <label for="member-checkbox">
            <input type="checkbox" name="member-checkbox" id="member-checkbox" value="yes" <?php if ( isset ( $member_page_circles_stored_meta['member-checkbox'] ) ) checked( $member_page_circles_stored_meta['member-checkbox'][0], 'yes' ); ?> />
            <?php _e( 'Member Client', 'member_circles_textdomain' )?>
        </label><br />
        <label for="phone-checkbox">
            <input type="checkbox" name="phone-checkbox" id="phone-checkbox" value="yes" <?php if ( isset ( $member_page_circles_stored_meta['phone-checkbox'] ) ) checked( $member_page_circles_stored_meta['phone-checkbox'][0], 'yes' ); ?> />
            <?php _e( 'Phone Client', 'member_circles_textdomain' )?>
        </label><br />
        <label for="location-checkbox">
            <input type="checkbox" name="location-checkbox" id="location-checkbox" value="yes" <?php if ( isset ( $member_page_circles_stored_meta['location-checkbox'] ) ) checked( $member_page_circles_stored_meta['location-checkbox'][0], 'yes' ); ?> />
            <?php _e( 'Multiple Locations', 'member_circles_textdomain' )?>
        </label><br />
        <label for="second-entity">
            <input type="checkbox" name="second-entity" id="second-entity" value="yes" <?php if ( isset ( $member_page_circles_stored_meta['second-entity'] ) ) checked( $member_page_circles_stored_meta['second-entity'][0], 'yes' ); ?> />
            <?php _e( 'Multiple Entities', 'member_circles_textdomain' )?>
        </label><br />
        <label for="inactive-member">
            <input type="checkbox" name="inactive-member" id="inactive-member" value="yes" <?php if ( isset ( $member_page_circles_stored_meta['inactive-member'] ) ) checked( $member_page_circles_stored_meta['inactive-member'][0], 'yes' ); ?> />
            <?php _e( 'Inactive Member', 'member_circles_textdomain' )?>
        </label>
    </div>
</p>   

    <?php
}

/**
 * Saves the custom meta input
 */
function member_page_circles_meta_save( $post_id ) {

    // Checks save status - overcome autosave, etc.
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'member_page_circles_nonce' ] ) && wp_verify_nonce( $_POST[ 'member_page_circles_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

    // Exits script depending on save status
    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        return;
    }

// Checks for input and saves - save checked as yes and unchecked at no
if( isset( $_POST[ 'member-checkbox' ] ) ) {
    update_post_meta( $post_id, 'member-checkbox', 'yes' );
} else {
    update_post_meta( $post_id, 'member-checkbox', 'no' );
};
if( isset( $_POST[ 'phone-checkbox' ] ) ) {
    update_post_meta( $post_id, 'phone-checkbox', 'yes' );
} else {
    update_post_meta( $post_id, 'phone-checkbox', 'no' );
};
if( isset( $_POST[ 'location-checkbox' ] ) ) {
    update_post_meta( $post_id, 'location-checkbox', 'yes' );
} else {
    update_post_meta( $post_id, 'location-checkbox', 'no' );
};
if( isset( $_POST[ 'second-entity' ] ) ) {
    update_post_meta( $post_id, 'second-entity', 'yes' );
} else {
    update_post_meta( $post_id, 'second-entity', 'no' );
};
if( isset( $_POST[ 'inactive-member' ] ) ) {
    update_post_meta( $post_id, 'inactive-member', 'yes' );
} else {
    update_post_meta( $post_id, 'inactive-member', 'no' );
};

}
add_action( 'save_post', 'member_page_circles_meta_save' );

Also, here's the wp_list_pages() code from wp_includes folder

function wp_list_pages( $args = '' ) {
    $defaults = array(
        'depth' => 0, 'show_date' => '',
        'date_format' => get_option( 'date_format' ),
        'child_of' => 0, 'exclude' => '',
        'title_li' => __( 'Pages' ), 'echo' => 1,
        'authors' => '', 'sort_column' => 'menu_order, post_title',
        'link_before' => '', 'link_after' => '', 'walker' => '',
    );

    $r = wp_parse_args( $args, $defaults );

    $output = '';
    $current_page = 0;

    // sanitize, mostly to keep spaces out
    $r['exclude'] = preg_replace( '/[^0-9,]/', '', $r['exclude'] );

    // Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array)
    $exclude_array = ( $r['exclude'] ) ? explode( ',', $r['exclude'] ) : array();

    /**
     * Filters the array of pages to exclude from the pages list.
     *
     * @since 2.1.0
     *
     * @param array $exclude_array An array of page IDs to exclude.
     */
    $r['exclude'] = implode( ',', apply_filters( 'wp_list_pages_excludes', $exclude_array ) );

    // Query pages.
    $r['hierarchical'] = 0;
    $pages = get_pages( $r );

    if ( ! empty( $pages ) ) {
        if ( $r['title_li'] ) {
            $output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
        }
        global $wp_query;
        if ( is_page() || is_attachment() || $wp_query->is_posts_page ) {
            $current_page = get_queried_object_id();
        } elseif ( is_singular() ) {
            $queried_object = get_queried_object();
            if ( is_post_type_hierarchical( $queried_object->post_type ) ) {
                $current_page = $queried_object->ID;
            }
        }

        $output .= walk_page_tree( $pages, $r['depth'], $current_page, $r );

        if ( $r['title_li'] ) {
            $output .= '</ul></li>';
        }
    }

    /**
     * Filters the HTML output of the pages to list.
     *
     * @since 1.5.1
     * @since 4.4.0 `$pages` added as arguments.
     *
     * @see wp_list_pages()
     *
     * @param string $output HTML output of the pages list.
     * @param array  $r      An array of page-listing arguments.
     * @param array  $pages  List of WP_Post objects returned by `get_pages()`
     */
    $html = apply_filters( 'wp_list_pages', $output, $r, $pages );

    if ( $r['echo'] ) {
        echo $html;
    } else {
        return $html;
    }
}
  • have you verified that each post has only a single meta entry for each key? also- it's maybe a better design to save meta when something is "on" and just delete when it is false. that way, something has a key or it doesn't, which is simpler than looking at the value of the key for every post. – Milo Nov 5 '16 at 15:36
  • How would I go about validating that? I know they only had one in the previous version, could they have duplicated somehow when I imported the XML? In which case, should I create new ones, or is there a way to clear the meta content? And the reason I have it set to save in both situations is because of how I'm using them. sometimes I need a value even if they're not selected and I needed checkboxes for easy clicking (needs to be easily accessible to people who aren't necessarily tech-savvy) – Trenton Moore Nov 5 '16 at 15:57
  • I've created a new page and added the same meta info and it does not seem to be duplicating. So I think you may be right about them having multiple entries. But I still don't know how to fix this. Do I have to delete the pages and recreate them from scratch? Admittedly that wouldn't be hard, but if there is a way I can just empty them somehow without losing the data, that would be much better. – Trenton Moore Nov 5 '16 at 16:18
  • You should be able to see the meta data in the custom fields meta box when editing a page, and you can delete it there. If you change your code to delete the meta data when something is unchecked, you will prevent this situation from occurring again. You can still have exactly the same UI with checkboxes for everything, you just need to change your logic to checking if something does or doesn't exist rather than checking the value. – Milo Nov 5 '16 at 18:48
  • why do you 'echo' wp_list_pages() - it should echo automatically with your parameters. – Michael Nov 5 '16 at 19:49

Your Answer

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

Browse other questions tagged or ask your own question.