Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using the query below in the index.php file as the main query, whenever I try to go to the 2nd page I get a 404 error, I tried using posts_per_page but to no avail.

<?php
    $args = array(
  'post_type' => array( 'post', 'videos', 'music' ),
  'tax_query' => array(
     array(
       'taxonomy' => 'content',
       'field'    => 'slug',
       'terms'    => 'indy',
       'operator' => 'NOT IN' 
     )
   )
);
        $products = new WP_Query( $args );
        if( $products->have_posts() ) {
            while( $products->have_posts() ) {
                $products->the_post();
                ?>

Do stuff here.

<?php }
        }
        else {
            echo 'Oh boy, no posts available';
        } ?>

What am I missing?

share|improve this question

3 Answers 3

up vote 3 down vote accepted

Pagination in URLs only works for the "main" query. What you've created is a secondary query, so WordPress doesn't "know" about it in such a way that the application would be able to create pagination. If you use a secondary query (like your example) it's up to you to do the pagination stuff manually.

Rather than create a new query, I'd suggest you alter the "main" query with the pre_get_posts hook. Example:

<?php
add_action('pre_get_posts', 'wpse89413_pre_posts');
function wpse89413_pre_posts($query)
{
    // make sure you're on the blog page and altering the main query
    if (is_home() && $q->is_main_query()) {
        $query->set('post_type', array('post', 'videos', 'music'));

        $query->set('tax_query', array(array(
           'taxonomy' => 'content',
           'field'    => 'slug',
           'terms'    => 'indy',
           'operator' => 'NOT IN' 
        )));
    }
}
share|improve this answer
    
I'm not sure how to mark this up, I tried working it into my query above but it either breaks the theme or returns a 404 on the homepage. –  Chozen Mar 6 '13 at 1:08

See Adding the $paged Parameter to a query in the codex. You'll want to do something like this:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; //or use the global $paged 

$args = array(
  'post_type' => array( 'post', 'videos', 'music' ),
  'posts_per_page' => -1, //-1 for all post that match query
   'paged' => $paged, //get current page 
   'tax_query' => array(
     array(
       'taxonomy' => 'content',
       'field'    => 'slug',
       'terms'    => 'indy',
       'operator' => 'NOT IN' 
     )
   )
);

//loop code 

However as Chris said it only works the the main query so you'll need to do:

        $temp = $wp_query; //save the main loop query for later use
        $wp_query= null; //set the current query to null
        $wp_query = new WP_Query($args); //get your args

 //do some sweet loop action

$wp_query = null; //set custom query to null
$wp_query = $temp; //rest main query back to the way things were
wp_reset_query(); //reset the main query

See this question or google custom query not working with pagination to find this same solution on other sites.

share|improve this answer
    
What makes my query secondary and how can I make it the main query? –  Chozen Mar 6 '13 at 1:13
    
It is secondary because it's the second query on the page. IE the main page loop we call the main query anything additional becomes secondary to that. You can use chrisguitarguy's solution to modify the main query on that page to your query. –  BandonRandon Mar 6 '13 at 3:47

Solved is, I got tired of tring to make it work in the index.php file and ended up using the funtions.php file instead.

I did the following.

add_action( 'pre_get_posts', 'add_my_custom_post_type' );

function add_my_custom_post_type( $query ) {
    if ( is_home() && $query->is_main_query() )
        $query->set( 'post_type', array( 'post', 'music', 'videos' ) );
        $query->set('tax_query', array(array(
           'taxonomy' => 'content',
           'field'    => 'slug',
           'terms'    => 'indy',
           'operator' => 'NOT IN' 
        )));
    return $query;
}

Pagination and filtering work perfectly.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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