0

I have a select option and some inputs that are getting into the $_GET, then based on $_GET, the $args of wp_query(args) will be changed, everything is fine and pagination works good. but when i change options or inputs it goes to the same page as the last select option and inputs was. for example if i choose options1 and go to page 2 it goes to page2 (no problem), but if i change options1 to options2 it goes to page 2 of options2 instead of page 1 of options2. I would be really thankful if somebody helps me.

 $today = date( 'Y-m-d' );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$cat= $_GET['select2'];
$tag= $_GET['tag'];
$state= $_GET['state'];
$city= $_GET['city'];
$keyword= $_GET['keyword'];
if ($state=='0') {
    $meta_query = array(
        array(

            'key' => 'date_added',
            'value' => $today,
            'compare' => '>=',
            'type' => 'DATE',

    ),
);
}
elseif ($state!='0'&& $city=='0'){
    $meta_query = array(
        'relation' => 'AND',
        array(
            'key' => 'state',
            'value' => $state,
        ),
        array(
            'key' => 'date_added',
            'value' => $today,
            'compare' => '>=',
            'type' => 'DATE',
        )
    );
}
elseif ($state!='0'&& $city!='0'){
    $meta_query = array(
        'relation' => 'AND',
        array(
            'key' => 'state',
            'value' => $state,
        ),
        array(
            'key' => 'city',
            'value' => $city,
        ),
        array(
            'key' => 'date_added',
            'value' => $today,
            'compare' => '>=',
            'type' => 'DATE',
        )
    );

}
if ($state=='0') {
    $meta_query_date_expiration = array(
        array(

            'key' => 'date_expiration',
            'value' => $today,
            'compare' => '>=',
            'type' => 'DATE',

        ),
    );
}
elseif ($state!='0'&& $city=='0'){
    $meta_query_date_expiration = array(
        'relation' => 'AND',
        array(
            'key' => 'state',
            'value' => $state,
        ),
        array(
            'key' => 'date_expiration',
            'value' => $today,
            'compare' => '>=',
            'type' => 'DATE',
        )
    );
}
elseif ($state!='0'&& $city!='0'){
    $meta_query_date_expiration = array(
        'relation' => 'AND',
        array(
            'key' => 'state',
            'value' => $state,
        ),
        array(
            'key' => 'city',
            'value' => $city,
        ),
        array(
            'key' => 'date_expiration',
            'value' => $today,
            'compare' => '>=',
            'type' => 'DATE',
        )
    );

}

$args= array(
    'post_type'=>'finmagevent',
    'posts_per_page' => 8,
    'paged' => $paged,
    'finmagevent_category' => $cat,
    'tag' => $tag,
    's'=>$keyword,
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'date_added',
            'value' => $today,
            'compare' => '>=',
            'type' => 'DATE',
        )
    )
);
   if(isset($_GET['select1'])){
    $select1 = $_GET['select1'];
    switch ($select1) {
        case 'value1':
             $args= array(
                'post_type'=>'finmagevent',
                'posts_per_page' => 8,
                'paged' => $paged,
                'finmagevent_category' => $cat,
                'tag' => $tag,
                's'=>$keyword,
                'orderby' => 'meta_value',
                'order' => 'ASC',
                'meta_query' => $meta_query,
        );
        break;
        case 'value2':
            $args= array(
                'post_type'=>'finmagevent',
                'posts_per_page' => 8,
                'paged' => $paged,
                'finmagevent_category' => $cat,
                'tag' => $tag,
                's'=>$keyword,
                'orderby' => 'meta_value',
                'order' => 'DSC',
                'meta_query' => $meta_query_date_expiration,
            );
            break;
        case 'value3':

         $args= array(
                'post_type'=>'finmagevent',
                'posts_per_page' => 8,
                'paged' => $paged,
                'finmagevent_category' => $cat,
                'tag' => $tag,
                's'=>$keyword,
            );
            echo $paged;?><br/><?php
            break;
        case 'value4':
            $args= array(
                'post_type'=>'finmagevent',
                'posts_per_page' => 8,
                'paged' => $paged,
                'finmagevent_category' => $cat,
                'tag' => $tag,
                'order' => 'ASC'
            );?><br/><?php
            break;
        default:
            break;
    }
}
    $data= new WP_Query($args);
    if($data->have_posts()) :
        while($data->have_posts())  : $data->the_post();
            ?>
            <div class="finmageventmain">
                <div class="finmageventthumbnail">
                    <?php
                    the_post_thumbnail('small');?>
                </div>
                <br>
                <div class="finmageventtitle"><h2>
                        <?php
                        the_title(); ?>
                    </h2>
                </div>
                <br>
                <p>
                <div class="finmageventdate finmageventdateadded"> <span>تاریخ برگزاری: <?php
                        $eventdate= get_post_meta(get_the_ID(),'date_added',true );
                        $eventdate_y=$eventdate[0].$eventdate[1].$eventdate[2].$eventdate[3];
                        $eventdate_m=$eventdate[5].$eventdate[6];
                        $eventdate_d=$eventdate[8].$eventdate[9];
                        $eventdate_jalali =gregorian_to_jalali ($eventdate_y,$eventdate_m,$eventdate_d,$mod='');
                        echo $eventdate_jalali[0].'/'.$eventdate_jalali[1].'/'.$eventdate_jalali[2]; ?>
                    </span></div>
                <br>

                <div class="finmageventdate"> <span>آخرین مهلت ثبت نام: <?php
                        $eventregisterdate= get_post_meta(get_the_ID(),'date_expiration',true );
                        $eventregisterdate_y=$eventregisterdate[0].$eventregisterdate[1].$eventregisterdate[2].$eventregisterdate[3];
                        $eventregisterdate_m=$eventregisterdate[5].$eventregisterdate[6];
                        $eventregisterdate_d=$eventregisterdate[8].$eventregisterdate[9];
                        $eventregisterdate_jalali =gregorian_to_jalali ($eventregisterdate_y,$eventregisterdate_m,$eventregisterdate_d,$mod='');
                        echo $eventregisterdate_jalali[0].'/'.$eventregisterdate_jalali[1].'/'.$eventregisterdate_jalali[2];
                        ?></span> </div>
                </p>
                <hr>
                <div class="finmageventcontent">
                    <?php the_excerpt();?>
                </div>
                <hr>
                <div class="finmageventproof"> <a class="finmageventkey" href="<?php echo get_post_meta( get_the_ID(), 'source', true ); ?>" target="_blank" id="link">ثبت نام</a> </div>
                <div class="finmageventsource"><a class="finmageventkey" href="<?php echo get_permalink(); ?>" target="_blank" id="link">نمایش جزئیات</a></div>
                <div><span>برگزار کننده: <?php echo get_post_meta( get_the_ID(), 'Organizer', true ); ?></span></div>
            </div>
        <?php
            wp_reset_postdata();
        endwhile;
        ?>
        <br>
        <?php
        $total_pages= $data->max_num_pages;
        if ($total_pages > 1){
            $current_page = max(1, get_query_var('paged'));
            $big = 99999; // need an unlikely integer
            echo paginate_links(array(
                'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                'format' => '/page/%#%',
                'current' => $current_page,
                'total' => $total_pages,
                'prev_text'    => __('« قبلی'),
                'next_text'    => __('بعدی »'),
            ));
        }
        ?>
    <?php else :?>
        <h3><?php _e('رویدادی یافت نشد', ''); ?></h3>
    <?php endif; ?>

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.