1

I have a custom post type called rock with a custom taxonomy called genero. taxonomy-genero-curiosity.php

mypage/Rock/curiosity/

When i click subsequent paginated page links (e.g. [2], [3], [next])

mypage/Rock/curiosity/page/2

takes me to an 404 page.

    <?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
//for a given post type, return all
$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'genero', 
            'field' => 'slug', 
            'terms' => 'curiosity' 
        )
    ),
    'post_type'=>'',    //add your post type name 
    'posts_per_page' => 1, 
);
    $my_query = null;
    $my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
  endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>
 <?php wp_pagenavi( array( 'query' => $my_query ) ); ?>
            <?php wp_reset_postdata(); ?>

How can I fix this? What am I doing wrong?

EDIT

Here is how I register my post type

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'Rock', 'post type general name' ),
    'singular_name'      => _x( 'Rock', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'entry in rock' ),
    'add_new_item'       => __( 'Add New entry in rock' ),
    'edit_item'          => __( 'Modificar entry in rock' ),
    'new_item'           => __( 'New entry in rock' ),
    'all_items'          => __( 'all entry in rock' ),
    'view_item'          => __( 'see entry in rock' ),
    'search_items'       => __( 'search entry in rock' ),
    'not_found'          => __( 'Nothing entry in rock' ),
    'not_found_in_trash' => __( 'Nothing entry in rock on thrash' ),
    'parent_item_colon'  => '',
    'menu_icon' => get_bloginfo('template_directory'). '/images/my_menu_icon.png',
    'menu_name'          => 'Rock'
  );

  $args = array(
    'labels'        => $labels,
    'description'   => 'Introduzca un nueva entrada en rock',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
    'hierarchical'  => true,
    'rewrite'   => array('slug' => 'Rock/%genero%', 'with_front' => false),
    'query_var'   => true,
    //'rewrite'   => true,
    //'publicly_queryable' => false,
  );
  register_post_type( 'Rock', $args );
}
add_action( 'init', 'my_custom_post_product' );

And here is my loop as it stands now. I still get the 404 error with this code

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <header>
      <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
      <p><time datetime="<?php echo get_the_date(); ?>"><?php echo get_the_date(); ?></time>. <?php if ( comments_open() ) : ?><a class="comment" href="<?php the_permalink(); ?>#comments"><?php comments_number('0 Comments', '1 Comment', '% Comments'); ?></a><?php endif; ?></p>
    </header>

<?php endwhile; else: ?>
  <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>


<nav class="paging">
  <?php if(function_exists('wp_pagenavi')) : wp_pagenavi(); else : ?>
    <div class="prev"><?php next_posts_link('&laquo; Previous Posts') ?></div>
    <div class="next"><?php previous_posts_link('Next Posts &raquo;') ?></div>
  <?php endif; ?>
</nav>

EDIT 2

I've changed camelcase "Rock" for "rock" and works, but only if change the configuration in Admin > Settings > Reading and set post to display to 5 like the number of posts_per_page (on the loop), but if i need show less post on other page for example post_per_page=3 (e.g. [2], [next]) appears 404 error page.

Is there a way to customize the number if required on other pages?

<?php $posts=query_posts($query_string . '&orderby=asc&posts_per_page=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>
 <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?$content = get_the_excerpt();
echo substr($content, 0, 200);
global $post;
$text = $post->post_excerpt;?>

<?php endwhile; else: ?>

  <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
  • You have a lot of issues with your code, and why are you using a custom query when the main query will do – Pieter Goosen Dec 4 '14 at 5:15
  • Sorry Pieter, I did not quite understand the question, my problem is takes me to an 404 page on mypage/Rock/curiosity/page/2 page links (e.g. [2], [next]) I do not know if the problem might not find the page that returns the loop or Rewrite p.s 10 hours ago I'm trying to solve the problem :( – user3766356 Dec 4 '14 at 6:18
  • where do you add your custom taxonomy? – Milo Dec 4 '14 at 14:14
0

Your problem is definitly your custom query. There are a problem or two with your custom query which I will not handle now. You should work through this page (WP_Query) on how to properly construct a custom query

Your real issue here is, and the question you must ask youself, is the custom query necessary? And the straight answer should be a solid NO. Custom queries should always be avoided in favor of the main query on the home page and any type of archive page. The main query is very specific to the particular page being requested, something that is not replicated by your custom query.

Although these custom queries works, the real issues comes in when you try to paginate, as you have experienced now.

The basic layout with the default loop to display the main query's results of any template should look like this

if( have_posts() ) {
    while( have_posts() ) {
        the_post();

        // CALL YOUR TEMPLATE TAGS

    }
} 

The if condition is not always necessary like on the single.php template and archive templates. As you can see, NO custom queries.

Any changes that you need to make to the main query ( except on page templates where you will need to run a custom query) can be done via pre_get_posts which is the correct way of changing what is being queried on a template. For additional info on the above said, check this post

So, your solution would be, delete all parts of your custom query, and replace it with the default loop as I have given above, just add your loop objects and HTML mark up to that, and then you can paginate as normal

EDIT

From the code from your pastes, your query looks fine now. That is how it should be.

Your root cause of all of this is your post type, Rock. You cannot use camelcase in custom post type names (this goes for custom taxonomy and function names). You should consider renaming your custom post type to rock.

And remember, flush and reflush your permalinks to make sure your changes has taken effect after you have updated your custom post type code

EDIT 2

You either did not read the linked posts or you just took a suicidal shortcut with query_posts. Please reread the linked post in this answer. You should NEVER ever make use of query_posts.

Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

Delete this line

 $posts=query_posts($query_string . '&orderby=asc&posts_per_page=5'); 

This line is the root of your evil on your page

You can change pagination on a per page basis with pre_get_posts which is the correct way of doing this. If you need to change the number of posts on just your curiosity term page, you can add the following to your functions.php (Note: this requires PHP 5.3+)

add_action( 'pre_get_posts', function ($q ) {
    if( !is_admin() && $q->is_main_query() && $q->is_tax( 'genero', 'curiosity' ) ) {
       $q->set( 'posts_per_page', 3 );
       $q->set( 'order', 'ASC' );
    }
)};

EDIT 3

For versions prior to PHP 5.3, the above won't work as anonymous functions was introduced only in PHP 5.3. For these versions, try the following

add_action( 'pre_get_posts', 'wpse170243_custom_ppp' );
function wpse170243_custom_ppp($q ) {
    if( !is_admin() && $q->is_main_query() && $q->is_tax( 'genero', 'curiosity' ) ) {
       $q->set( 'posts_per_page', 3 );
       $q->set( 'order', 'ASC' );
    }
}
  • if is that I understood, then recently changed the code for: pastebin.com/1isYZ209# it's okay? but anyway give me error 404 page. mypage/Rock/curiosity/page/2 this is the code from my custom post type function pastebin.com/2cNG7Kuj – user3766356 Dec 4 '14 at 7:41
  • Well the progress i changed to camelcase "Rock" for "rock" and works :) !, but only if change the configuration in Admin > Settings > Reading and set post to display to 5 like the number of posts_per_page (on the loop), but if i need show less post on other page for example post_per_page=3 (e.g. [2], [next]) appears 404 error page, There is a way to customize the number if required on other pages? Loop: pastebin.com/AckuKeH5 – user3766356 Dec 4 '14 at 22:59
  • Please see my update. That is why is is so important to to a.) Reply if an answer don't work so the answerer can assist you further in solving your issue :-) b.) Accept the appropriate answer that helped you solve the problem so that everybody know the issue is solved and that a specific answer solved it. :-) – Pieter Goosen Dec 5 '14 at 4:34
  • i undestand then delete the line that contain query_posts. and add your code in functions.php but appears Parse error: parse error in functions.php on line 542 the line is add_action( 'pre_get_posts', function ($q) { – user3766356 Dec 5 '14 at 5:14
  • Which php version are you using. This only works on php 5.3+ – Pieter Goosen Dec 5 '14 at 5:21
1

Try calling the nav before you reset the query.

...

if (function_exists('wp_pagenavi')) wp_pagenavi(array('query' => $my_query));

wp_reset_query();  // Restore global post data stomped by the_post().

?>
  • does not work, when I try to go to next-page [2], takes me to an 404 page – user3766356 Dec 3 '14 at 23:13
0

Try to this:

Add This custom_pagination() function to your function.php file

 function custom_pagination($numpages = '', $pagerange = '', $paged='') {

  if (empty($pagerange)) {
    $pagerange = 2;
}

  global $paged;
  if (empty($paged)) {
    $paged = 1;
    }
  if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    }
}
  /** 
   * We construct the pagination arguments to enter into our paginate_links
   * function. 
   */
  $pagination_args = array(
    'base'               => get_pagenum_link(1) . '%_%',
    'total'              => $numpages,
    'current'            => $paged,
    'show_all'           => False,
    'end_size'           => 1,
    'mid_size'           => $pagerange,
    'prev_next'          => False,
    'prev_text'          => __('&laquo;' , 'swift'),
    'next_text'          => __('&raquo;' , 'swift'),
    'type'               => 'array',
    'add_args'           => false,
    'add_fragment'      => ''
  );

  $paginate_links = paginate_links($pagination_args);
  echo '<div class="Pagination-Num"><ul>';
        foreach ( $paginate_links as $paginate_link ) {
                 echo "<li> $paginate_link </li>";
            }
        echo '</ul></div>';

}

And Call custom_pagination() to your Post loop file

<?php
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    //for a given post type, return all
    $args = array(
        'tax_query' => array(
            array(
                'taxonomy' => 'genero', 
                'field' => 'slug', 
                'terms' => 'curiosity' 
            )
        ),
        'post_type'=>'',    //add your post type name 
        'posts_per_page' => 1, 
    );
        $my_query = null;
        $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    <?php
      endwhile;

     if (function_exists(custom_pagination)) {
        custom_pagination($my_query->max_num_pages,"",$paged);
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

      <?php wp_reset_postdata(); ?>

I think it will work fine

-1

Is your loop running on the homepage?

Add this to your functions.php and test.

   <?php function custom_loop_query( $query ) {
    if(  ! is_admin() && $query->is_main_query() ) {
    $query->set( 'post_type', array( 'rock' ) );
    }
    }
    add_action( 'pre_get_posts', 'custom_loop_query' );  
    ?>
  • Ok, please test with the code I posted in my reply. – Jinson Abraham Dec 4 '14 at 4:05
  • does not work,I try to go to next-page [2], takes me to an 404 page :( – user3766356 Dec 4 '14 at 4:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.