0

I have a problem with the pagination its working great with blog page, but when i want to show custom post type it shows always two pages even if it contains just one post.

P.S. I modified in my function to display pages on select option but before that it was the same problem.

enter code here
if ( !function_exists( 'wpbeginner_numeric_posts_nav' ) ) {
function wpbeginner_numeric_posts_nav() {

if( is_singular() )
    return;

global $wp_query;

/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
    return;

$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max   = intval( $wp_query->max_num_pages );

/** Add current page to the array */
if ( $paged >= 1 )
    $links[] = $paged;

/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
    $links[] = $paged - 1;
    $links[] = $paged - 2;
}

if ( ( $paged + 2 ) <= $max ) {
    $links[] = $paged + 2;
    $links[] = $paged + 1;
}

echo '<div class="navigation"><h3 class="filtttl">Pages</h3> ';?>
<form action="<?php bloginfo('url'); ?>" method="get" class="filterform page">
<input type="hidden" name="post_type" value="voyage" />
<input type="hidden" id="s" name="s" value="" />
<select name="paged" id="pageselect" onchange="form.submit()">
<?php echo ' . "\n"';

/** Previous Post Link */
/*if ( get_previous_posts_link() )
    printf( '<option>%s</option>' . "\n", get_previous_posts_link() );*/

/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
    $class = 1 == $paged ? ' class="active"' : '';

    printf( '<option%s><a href="%s">%s</a></option>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );

    if ( ! in_array( 2, $links ) )
        echo '<option>…</option>';
}

/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
    $class = $paged == $link ? ' class="active"' : '';
    printf( '<option%s><a href="%s">%s</a></option>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}

/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
    if ( ! in_array( $max - 1, $links ) )
        echo '<option>…</option>' . "\n";

    $class = $paged == $max ? ' class="active"' : '';
    printf( '<option%s><a href="%s">%s</a></option>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}

/** Next Post Link */
/*if ( get_next_posts_link() )
    printf( '<option>%s</option>' . "\n", get_next_posts_link() );*/

echo '</select></form></div>' . "\n";

}
}

And in search page thats my code.

enter code here
<div class="intro">
        <div class="leftsearchform">
            <form method="get" role="search" action="<?php bloginfo('url'); ?>" class="searchpage">
            <h2 class="srchttl">Rechercher votre voyage</h2>
                <input type="text" class="dest" name="dest" placeholder="Destination" />
                <input type="text" class="dep" name="dep" placeholder="Départ" />
                <input type="text" class="arr" name="arr" placeholder="Arrivée" />
                <input type="hidden" name="post_type" value="voyage" />
                <input type="hidden" id="s" name="s" value="" />
                <input type="submit" class="sub" value="Rechercher" />
            </form>
        </div><!-- !leftsearchform -->
        <div class="resultsearch">

            <div class="filtersrch">
                <?php $sort= $_GET['sort'];
                        if($sort == "Croissant")
                        {
                            $filter= "asc";
                        }
                        if($sort == "Decroissant")
                        {
                            $filter= "desc";
                        }
                        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                ?>
                <?php $temp = $voy_query; $voy_query= null;
                $args = array( 'post_type' => 'voyages', 'posts_per_page' => 4, 'orderby' => 'meta_value_num', 'meta_key' => $prx, 'order' => $filter, 'paged' => $paged, 's' =>$s, );
                $voy_query = new WP_Query($args);  ?>
                <h3 class="nbrrzlt"><?php echo $voy_query ->found_posts .' résultats.'; ?></h3>
                    <h3 class="filtttl">Prix</h3>
                    <form action="" method="get" class="filterform">
                    <!--<input type="hidden" name="post_type" value="voyage" />-->
                    <input type="hidden" id="s" name="s" value="" />
                        <select name="sort" id="sorting" onchange="form.submit()">
                            <option value="Croissant" <?php if ($sort == "Croissant"){ echo 'selected="selected"'; } ?> >
                            Croissant</option>
                            <option value="Decroissant" <?php if ($sort == "Decroissant"){ echo 'selected="selected"'; }?> 
                            selected="selected" >Decroissant</option>
                        </select>
                    </form>
                    <?php wpbeginner_numeric_posts_nav(); ?>
            </div><!-- !filtersrch -->
            <?php 
                if ($voy_query->have_posts()) : while ($voy_query->have_posts()) : $voy_query->the_post();
                    $post_id = get_the_ID();

EDIT:

// Load our function when hook is set
add_action( 'pre_get_posts', 'rc_modify_query_get_design_projects' );
function rc_modify_query_get_design_projects( $query ) {


// Check if on frontend and main query is modified
if( ! is_admin() && $query->is_main_query() && $query->query_vars['post_type'] = 'voyages' ) {
    if ($query->is_search) {
    $query->set('meta_key', $prx);
    $query->set('meta_value', 'voyage[prx]');
    }
}
}

EDIT2:

function textdomain_include_search($query) {
if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_search) {
        $query->set('post_type', array( 'post', 'voyages' ) );
    }
}
}
add_action( 'pre_get_posts', 'textdomain_include_search' );
  • Use pre_get_posts to alter the main query. Your pagination is calculated from the main query. Your main query have more posts than the custom one, that is why you get two pages – Pieter Goosen Oct 31 '15 at 13:08
  • @Pieter Goosen Thank you for this answer, can you see my edit if its correct because now the pagination doesn't show. – Carl Willis Oct 31 '15 at 14:10
  • Where do you set the value of $prx? – Milo Nov 1 '15 at 0:55
  • @Milo $prx is a metabox but i added it just because i want to order by it. But for pagination i guess I missed something. the pre_get_filter is it correct and what i need to do in the query after that. – Carl Willis Nov 1 '15 at 10:37
  • @pieter-goosen I added a new edit about the last function that i create but the pagination won't show so did i need to add something in search page. – Carl Willis Nov 1 '15 at 11:36

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.