I am working on a theme with custom post type.
the issue currently is that i am saving the layout files in a folder called templates.. and in front-page.php i am using an options check to see which type of layout did the user selected from theme panel.. and include that file..
Right now in the custom post type template, pagination isn't working.. i am using this loop :

    global $wp_query;

$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
                if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); 
    <?php endwhile; endif; ?>
                if (function_exists("pagination")) {
    <?php $wp_query = null; $wp_query = $temp;?>

The pagination shows up but shows link as /page/2/ .. which gives 404 on running.. in my opinion it is showing the main query's page 2, as not many posts added so giving 404.

I have also tried :

$paged = 1;  
if ( get_query_var('paged') ) $paged = get_query_var('paged');  
if ( get_query_var('page') ) $paged = get_query_var('page');    

Didn't work..
My Custom Post Register Structure :

function my_custom_post_portfolio() {
    $labels = array(
        'name'               => _x( 'Portfolio', 'post type general name' ),
        'singular_name'      => _x( 'Portfolio', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'book' ),
        'add_new_item'       => __( 'Add New Item' ),
        'edit_item'          => __( 'Edit Item' ),
        'new_item'           => __( 'New Portfolio Item' ),
        'all_items'          => __( 'All Items' ),
        'view_item'          => __( 'View Items' ),
        'search_items'       => __( 'Search Portfolio' ),
        'not_found'          => __( 'No Portfolio found' ),
        'not_found_in_trash' => __( 'No Portfolio found in the Trash' ), 
        'parent_item_colon'  => '',
        'menu_name'          => 'Portfolio'
    $args = array(
        'labels'        => $labels,
        'description'   => 'Holds our Portfolio and product specific data',
        'public'        => true,
        'menu_position' => 5,
        'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
        'has_archive'   => true,
        'rewrite' => array( 'slug' => 'portfolio', 'with_front' => true ),
        '_builtin' =>  false, // It's a custom post type, not built in!
        'query_var' => true,
        'taxonomies' => array('portfolio', 'post_tag') // this is IMPORTANT

    register_post_type( 'Portfolio', $args );   
add_action( 'init', 'my_custom_post_portfolio' );  

Help is appreciated! Thanks :)


1 Answer 1


It is hard to say with 100% certainty but I think the problem is your use of query_posts. Here is what I believe is happening:

  1. The Main query runs with the default parameters and loads your page
  2. You clobber the main query with query_posts
  3. You generate pagination based on the clobbered main query
  4. You click a link
  5. The unclobbered main query tries to sort out what you want (remember, your query_posts can't run until WordPress loads the template)
  6. And fails and you get a 404

Just don't use query_posts. If you ever think you have found a legitimate place to use query_posts you are almost certainly wrong. The Core is the only thing that should be running that function.

If you must alter the main query use a filter on pre_get_posts.

function alter_query_wpse_99042($qry) {

However, based on your post type registration settings it looks like WordPress should be building the archive for you. I am not sure why you need to be constructing a query at all, except maybe to change posts_per_page. You can customize the generated archive by making a file named archive-portfolio.php and saving it to the theme directory. single-portfolio.php does the same with single post-type displays. Those files are loaded automatically.

  • Hello, thanks it fixed the issue but break everything else, i mean what u did is altered the main query, as i am using 4 different templates.. it is making all queries post type as portfolio. so as i gathered from your answer, what i need to do is not use query_post. let me try it and get back to you
    – Ayaz Malik
    Commented May 10, 2013 at 19:59
  • That is sample code. You will need to add the appropriate logic to make sure it works only where you want it to.
    – s_ha_dum
    Commented May 10, 2013 at 20:05
  • just updated the question with custom query.. can you please check. and now pagination stopped showing up.
    – Ayaz Malik
    Commented May 10, 2013 at 20:07
  • That is not what I said to do. Some (most) of the pagination functions operate on the global $wp_query object. See: wordpress.stackexchange.com/questions/77661/… Also see: wordpress.stackexchange.com/questions/92520/…
    – s_ha_dum
    Commented May 10, 2013 at 20:12
  • 1
    Still, those are workarounds. The best way to do this is to alter the query with pre_get_posts. I think you are charging along in the wrong direction.
    – s_ha_dum
    Commented May 10, 2013 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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