Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Taxonomy Archive only showing first 2 pages, then shows 404 WordPress

  • SOLVED

I have a taxonomy template for my tax specific archive.

So the code below is on my <strong>taxonomy-group.php</strong> template.


<?php

global $wp_query, $paged;

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(
'paged' => $paged,
'posts_per_page' => 2
);

if ($user_media) {

array_merge($args, array(
'meta_query' => array(
array(
'key' => 'download_access_rules',
'value' => 'genpo',
'compare' => '!='
)
)
));

}

$new_args = array_merge( $wp_query->query_vars, $args );
query_posts( $new_args );

if ( have_posts() ) : ?>

<div class="row">

<div id="hrc-downloads" class="col-xs-20">

<div class="row">

<?php $count = 0; ?>

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

get_template_part('content-item-download');

endwhile; ?>

</div>

</div>

</div>

<?php wp_pagenavi(); ?>

<?php endif; wp_reset_query(); ?>


Now the problem I am having is that when using the page navigation in the footer (which is done using PageNavi plugin), is when navigate to any pages past page 2, I get and error 404?

Now I have tested my the page navigation on my archive.php template any exactly the same thing happens if I add a simple query_post to show 2 post per page. Again all pages past page 2 show error 404. See the code I add which causes the same problem...


<?php

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

global $query_string;
query_posts( $query_string . '&posts_per_page=2' );

if ( have_posts() ) :

?>

<?php $count = 0; ?>

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

if ( 'reports' == get_post_type() ) {

get_template_part('content-item-report');

} else {

get_template_part('content-item');

}

endwhile; ?>

<?php wp_pagenavi(); ?>

<?php endif; wp_reset_query(); ?>


This a causing me massive head ache.

Can anyone advise why this is happening.

<br/>

Thanks
Josh

Answers (2)

2014-01-27

Kailey Lampert answers:

Using query_posts can cause all sorts of oddities, it's recommended that you never use it.
The alternatives are using `new WP_Query` (when trying to create secondary post loops), or the pre_get_posts filter (for changing the main query). Looks like you should be using the latter.

Something like the following in your functions.php file.

add_action( 'pre_get_posts', 'my_altered_taxonomy_query' );

function my_altered_taxonomy_query( $query ) {

if ( is_admin() ) return;
if ( ! $query->is_main_query() ) return;
if ( ! is_tax( 'your-taxonomy' ) ) return;

$query->set( 'posts_per_page', 2 );
$query->set( 'meta_query', array(
array(
'key' => 'download_access_rules',
'value' => 'genpo',
'compare' => '!='
)
)
);

}


Kailey Lampert comments:

make sure you pass 2 parameters to $query->set();

add_action( 'pre_get_posts', 'download_tax_group_filter' );

function download_tax_group_filter( $query ) {

if ( is_admin() ) return;
if ( ! $query->is_main_query() ) return;
if ( ! is_tax('group') ) return;

$query->set( 'posts_per_page', 2 );

global $user_media;
if ( $user_media ) {
$query->set( 'meta_query', array(
array(
'key' => 'download_access_rules',
'value' => 'genpo',
'compare' => '!='
)
)
);
}
}


Josh Cranwell comments:

Thanks Kailey


This has been hand. Thinking I'm going to combine yours with arnavs :-)

2014-01-27

Arnav Joy answers:

try this


function be_group_query( $query ) {

if( !is_admin() && is_tax( 'group' ) ) {

$query->set( 'posts_per_page', '4' );
}

}

add_action( 'pre_get_posts', 'be_group_query' );


you can change things in this line

if( !is_admin() && is_tax( 'group' ) )

and in this line

$query->set( 'posts_per_page', '4' );


Josh Cranwell comments:

I'm trying but this is not working...

function download_tax_group_filter( $query ) {

global $user_media;

if( !is_admin() && is_tax( 'group' ) ) {

$args = array(
'posts_per_page' => 2
);

if ($user_media) {

array_merge($args, array(
'meta_query' => array(
array(
'key' => 'download_access_rules',
'value' => 'genpo',
'compare' => '!='
)
)
));

}

$query->set($args);
}
}
add_action( 'pre_get_posts', 'download_tax_group_filter' );


Josh Cranwell comments:


function download_tax_group_filter( $query ) {

global $user_media;

if( !is_admin() && is_tax( 'group' ) ) {

$args = array(
'posts_per_page' => 2
);

if ($user_media) {

array_merge($args, array(
'meta_query' => array(
array(
'key' => 'download_access_rules',
'value' => 'genpo',
'compare' => '!='
)
)
));

}

$query->set($args);
}
}
add_action( 'pre_get_posts', 'download_tax_group_filter' );


Arnav Joy comments:

what's the value of the reading settings in admin panel?


Josh Cranwell comments:

10 But I want my archive.php page to stay as 10.

Just need my taxonomy-group.php


Josh Cranwell comments:

As you can see with my code below , everything is in array format..


function download_tax_group_filter( $query ) {



global $user_media;



if( !is_admin() && is_tax( 'group' ) ) {



$args = array(

'posts_per_page' => 2

);



if ($user_media) {



array_merge($args, array(

'meta_query' => array(

array(

'key' => 'download_access_rules',

'value' => 'genpo',

'compare' => '!='

)
)
));
}
$query->set($args);
}
}
add_action( 'pre_get_posts', 'download_tax_group_filter' );




But in your code...

$query->set( 'posts_per_page', '4' );

The query parameters are separated by a comma... can you see what I mean?


Arnav Joy comments:

try this

function download_tax_group_filter( $query ) {

global $user_media;

if( !is_admin() && is_tax( 'group' ) ) {

if ($user_media) {

$meta_query = array(
array(
'key' => 'download_access_rules',
'value' => 'genpo',
'compare' => '!='
)
);


$query->set( 'meta_query', $meta_query );

$query->set( 'posts_per_page', '2' );

}
}

}

add_action( 'pre_get_posts', 'download_tax_group_filter' );


Josh Cranwell comments:

Ahh yes this works thanks.

How ever it was stopping my querys in the side bar from working. So I added this... <strong>&& is_main_query()</strong>

I guess this is the right thing to do? It seems to be working.

function download_tax_group_filter( $query ) {

global $user_media;

if( !is_admin() && is_tax( 'group' ) && is_main_query() ) {

if ($user_media) {

$meta_query = array(
array(
'key' => 'download_access_rules',
'value' => 'genpo',
'compare' => '!='
)
);

$query->set( 'meta_query', $meta_query );

}

$query->set( 'posts_per_page', '2' );

}
}
add_action( 'pre_get_posts', 'download_tax_group_filter' );


Arnav Joy comments:

yes it is ok.


Josh Cranwell comments:

Ok great thanks.

I think I going to use Kailey Lampert code. Half yours half hers.

Thanks