Set default sort order to be by ‘title’ and posts per page to ‘100’

Question

Need to change below code to have the default selection to be ‘by title’ and ‘100’ per page

function iknow_posts_sorter() {
    if ( $_GET && ! empty( $_GET ) ) {
        iknow_go_filter();
    }

    $sorterby = ! empty( $_GET['select'] ) ? sanitize_text_field( wp_unslash( $_GET['select'] ) ) : '';


    $sorter_arr = array(
        'newest'   => esc_attr__( 'newest', 'iknow' ),
        'title'    => esc_attr__( 'by title', 'iknow' ),
        /*'comments' => esc_attr__( 'by comments', 'iknow' ),*/
    );

    $posts = ! empty( $_GET['per_page'] ) ? absint( $_GET['per_page'] ) : 'default';

    $posts_arr = array(
        'default' => esc_attr__( 'Default', 'iknow' ),
        '20'      => '20 ' . esc_attr__( 'Per Page', 'iknow' ),
        '50'      => '50 ' . esc_attr__( 'Per Page', 'iknow' ),
        '100'     => '100 ' . esc_attr__( 'Per Page', 'iknow' ),

    );
    ?>
    <form method="get" id="order" class="level-right">
        <div class="level-item">
            <div class="field">
                <div class="control">
                    <div class="select is-small is-primary">
                        <select name="select" class="" onchange="this.form.submit();">
                            <?php
                            foreach ( $sorter_arr as $key => $val ) {
                                echo '<option value="' . esc_attr( $key ) . '" ' . selected( $key, $sorterby, false ) . '>' . esc_attr( $val ) . '</option>';
                            }
                            ?>

                        </select>
                    </div>
                </div>
            </div>
        </div>

        <div class="level-item">
            <div class="field">
                <div class="control">
                    <div class="select is-small is-primary">
                        <select name="per_page" class="" onchange="this.form.submit();">
                            <?php
                            foreach ( $posts_arr as $key => $val ) {
                                echo '<option value="' . esc_attr( $key ) . '" ' . selected( $key, $posts, false ) . '>' . esc_attr( $val ) . '</option>';
                            }
                            ?>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </form>

    <?php

}
0
Jay Caetano 4 months 0 Answers 14 views 0

Leave an answer