0

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?

2

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' 
        )));
    }
}
  • 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
0

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.

  • 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. – Brooke. Mar 6 '13 at 3:47
0

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.

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.