• Hi, guys!
    I have a terrible problem with my wp_query pagination for custom post type records .(

    I use code above to do my custom post loop output:

    <?php
    
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    $arg=array(
        'posts_per_page' => 5,
        'post_type' => 'doctor',
        'post_status' => 'publish',
        'paged' => $paged
    );
    
    $arg=apply_filter($arg);
    
    $posts = new WP_Query($arg);
    
    ?>
    
    <div class="row sort">
        <?php get_template_part('includes/sort') ?>
    </div>
    
    <?php
    
    if($posts->have_posts()) {
        while ($posts->have_posts()) {
            $posts->the_post();
            echo get_template_part('content', 'doctor');
        }
    }
    
    wp_reset_postdata();
    
    if(function_exists('custom_pagination')) custom_pagination($posts);
    
    ?>

    There is function to output my custom pagination by custom wp_query:

    function custom_pagination($posts) {
    	$big = 999999999;
    
    	$pages = paginate_links(array(
    		'base' => str_replace($big, '%#%', get_pagenum_link($big, false)),
    		'format' => '?paged=%#%',
    		'current' => max(1, get_query_var('paged')),
    		'total' => $posts->max_num_pages,
    		'type' => 'array',
    		'prev_next' => TRUE,
    		'prev_text' => '«',
    		'next_text' => '»',
    	));
    	if (is_array($pages)) {
    		$current_page = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
    		echo '<ul class="pagination">';
    		foreach ($pages as $i => $page) {
    			if ($current_page == 1 && $i == 0) {
    				echo "<li class='active'>$page</li>";
    			} else {
    				if ($current_page != 1 && $current_page == $i) {
    					echo "<li class='active'>$page</li>";
    				} else {
    					echo "<li>$page</li>";
    				}
    			}
    		}
    		echo '</ul>';
    	}
    }

    So, I create my pagination in this way.
    ..but I cant to change my current page to 2,3 or another, except – first page. I have got 404 error and I dont know why.

    Any ideas?

Viewing 1 replies (of 1 total)
  • Thread Starter roma-glushko

    (@roma-glushko)

    Oooh, yeah I forgot about apply_filter():

    function apply_filter($other){
    	$args = array(); // подготовим массив
    
        if($_GET) {
            $args['meta_query'] = array('relation' => 'AND'); // отношение между условиями, у нас это "И то И это", можно ИЛИ(OR)
            global $wp_query;
    
            if (!empty($_GET['fio'])) $args['s'] = sanitize_text_field($_GET['fio']);
    
            if (!empty($_GET['hospital']) && is_numeric($_GET['hospital']) && intval($_GET['hospital']) > 0) {
                $args['meta_query'][] = array(
                    'key' => 'doctor-hospital',
                    'value' => $_GET['hospital'],
                );
            }
    
            if (!empty($_GET['profession']) && is_numeric($_GET['profession']) && intval($_GET['profession']) > 0) {
                $args['meta_query'][] = array(
                    'key' => 'doctor-profession',
                    'value' => $_GET['profession'],
                );
            }
    
            if (!empty($_GET['sex']) && in_array($_GET['sex'], array('0', '1'))) {
                $args['meta_query'][] = array(
                    'key' => 'doctor-sex',
                    'value' => $_GET['sex'],
                );
            }
    
            if (!empty($_GET['range']) && in_array($_GET['range'], array('0', '1'))) {
                $args['meta_query'][] = array(
                    'key' => 'doctor-range',
                    'value' => $_GET['range'],
                );
            }
    
            if (!empty($_GET['title'])) {
                $args['meta_query'][] = array(
                    'key' => 'hospital-title',
                    'value' => sanitize_text_field($_GET['title']),
                    'compare' => 'LIKE',
                );
            }
    
            if (!empty($_GET['address'])) {
                $args['meta_query'][] = array(
                    'key' => 'hospital-address',
                    'value' => sanitize_text_field($_GET['address']),
                );
            }
    
            if (!empty($_GET['phone'])) {
                $args['meta_query'][] = array(
                    'key' => 'hospital-phone',
                    'value' => sanitize_text_field($_GET['phone']),
                );
            }
        }
    	return array_merge($args, $other);
    }
Viewing 1 replies (of 1 total)

The topic ‘WP_Query pagination page>2 not found(404 error)’ is closed to new replies.