Wordpress search term pagination broken - wordpress

I'm having an issue with the pagination in search archives. For some reason whenever I try going to the next page the pagination gives me a 404.
It's a default if (have_posts()) : while (have_posts()) : the_post(); loop.
I don't know how, but I think the reason is because I'm getting redirected to a new page that get's generated somehow. Say I search for stoff. Instead of /page/2/?s=stoff it replaces it with ?attachment_id=550&s=stoff.
Here's what I've tried to do (with no different results)
Change the search query (attachment_id is the same here as well)
Change pagination functions (pagenavi, get_next_posts_link, custom function)
Replacing loop content with just a title
removing header, sidebars, footer
resetting post data
Resetting permalinks (pretty)
You can test the pagination live here (dev location)
Any thoughts on this are greatly appreciated :)
EDIT -
Added Pagination code currently used in search.php
I don't think it should be a problem with the pagination, seeing that I've tried so many different ones, and this pagination still works in every other location at the site, but here it is:
// Numeric Page Navi (built into the theme by default)
function bones_page_navi() {
global $wp_query;
$bignum = 999999999;
if ( $wp_query->max_num_pages <= 1 )
return;
echo '<nav class="pagination">';
echo paginate_links( array(
'base' => str_replace( $bignum, '%#%', html_entity_decode( get_pagenum_link($bignum) ) ),
'format' => '',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => '←',
'next_text' => '→',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3
) );
echo '</nav>';
} /* end page navi */
The function get's called after the endwhile;

Related

Trouble with caching and 3 wp_query

In my custom theme in WordPress I have created a front-page.php. There I have three WP_Query's. The first one just takes a custom post type.
$args = array (
'post_type' => array( 'frontpage_element' ),
'orderby' => 'rand',
'posts_per_page' => '1',
);
$query = new WP_Query( $args );
if ($query->have_posts()):
while ($query->have_posts() ) : $query->the_post();
// DISPALY A PICTURE
endwhile;
// Reset postdata
wp_reset_postdata();
endif;
The second is the default Loop.
if (have_posts()) :
while (have_posts()) : the_post(); ?>
the_title();
the_content();
endwhile;
// Reset postdata
wp_reset_postdata();
endif;
The third WP_Query shows the first 6 post entries of a category.
$options = get_option('custom_theme_options');
$categories = implode("," , $options['catprojectlayout']);
$atts = shortcode_atts( array(
'paging' => 'projectpage',
'post_type' => 'post',
'posts_per_page' => '6',
'post_status' => 'publish',
'cat' => $categories,
'cache_results' => false
), $atts );
$paging = $atts['paging'];
unset( $atts['paging'] );
if( isset($_GET[$paging]) )
$atts['paged'] = $_GET[$paging];
else
$atts['paged'] = 1;
$custom_query = new WP_Query( $atts );
$pagination_base = add_query_arg( $paging, '%#%' );
if ($custom_query->have_posts()) : while ($custom_query->have_posts()) : $custom_query->the_post();
the_title();
the_content();
endwhile; endif;
echo paginate_links( array(
'type' => '',
'base' => $pagination_base,
'format' => '?'. $paging .'=%#%',
'current' => max( 1, $custom_query->get('paged') ),
'total' => $custom_query->max_num_pages,
'prev_text' => '<div class="prevbtn"></div>',
'next_text' => '<div class="nextbtn"></div>',
'show_all' => true
));
The code works, but I have troubles with caching. When I add a new Post to the defined category, the second WP_Query was not updated (I think because of browser caching).
I think I have the code done like is defined in WP Codex.
It should be work like this: When I add a new post in the defined category, a User browse to the front-page, the site must be reloaded from server (because in the third WP_Query there is a new entry).
it is ulikely to be browser caching as browsers tend to store style sheets and images page content is normally re-interpreted each time the page is loaded incase it has changed.
It is more likely to be either cashing on the server or another 3rd party service such as Cloud flare which store the page appearance for a period of time to save regenerating the same wedge of dynamic page controls over and over.
WP_Engine hosting has it's own caching that can be cleared through the installs dash board, Cloud flare allows cache to be cleared per URL and plugins... well these are all different, check if there are any plugins that control caching and either turn them off whilst you are making a change or clear their cache.

sectioning content within a wordpress page

Building a wordpress site that needs to organize content by stream - e.g Mechanical, Electrical etc. Also, each page needs sections like News, Articles events etc. If you pick one page (say Mechanical) it has to have the following sections
News
Articles (category:articles)
Events (category:events)
The other streams will have the same sections as well
Is there a plugin for achieving or would I be better off building a template page for each vertical and writing php code? Shown code for a page with a single section.
<?php
$args = array(
'posts_per_page' => 1,
'category_name' => 'news',
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args );
$the_query = new WP_Query($args);
//EXAMPLE NEWS SECTION
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_content();
echo $content;
}
} else {
// no posts found
}
?>
In my opinion you could just write a plugin which does that filtering.
Said plugin would have some kind on shortcode that would take a parameter (category for instance) and would return or echo all the posts associated with that category.
Shortcode registration :
add_action('init','register_shortcode');
function register_shortcode(){
add_shortcode('shortcode_name', 'shortcode_function');
}
Shortcode function:
function shortcode_function($attr){
$attr = shortcode_atts(array('category' => 'default'),$attr); //Setting defaults parameters
$args = array(
'category_name' => $attr['category'],
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//echo other html related stuff
the_title();
the_content();
}
}
}
Usage
[shortcode_name category='news']
[shortcode_name] //category defaults at 'default'
In your case a page could be something like that
<div>
News : <br/>
[shortcode_name category='news']
</div>
<div>
Articles : <br/>
[shortcode_name category='articles']
</div>
<div>
Events : <br/>
[shortcode_name category='events']
</div>
I don't think there is one right way to do this... it depends on the scenario... How many posts, how often do sections or "main streams" get added / change, do you need other stuff on the pages etc.
One pretty simple and easily maintained solution would be to:
1. Add mechanical, electrical, etc. as categories alongside news, articles and events
2. Modify (or add) category.php to repeat the loop three times, and only display the posts that belong to each section (other category):
//Loop your sections
foreach(array('news', 'articles', 'events') as $category) {
echo '<h2>' . $category . '</h2>';
//Loop posts
while ( have_posts() ) {
the_post();
//Check if post is in section (by slug!)
if ( in_category($category) ) {
the_title();
the_content();
}
}
//Start the loop over
rewind_posts();
}
Now just assign each post to one (or more) "parents", fx mechanical, and also to one (and only one) of news, articles or events...
If you go to the archive page of mechanical you get three sections
if you go to news you get all news (but also two empty sections, so you should of course check for that).
Note that this could be done using tags or even a custom taxonomy if you wanted, you'd just need to edit a different theme file - see the template hierarchy.
Beware though that this will not work very nicely with pagination, so if that is a concern you will need to deal with that.

Trouble with pagination wordpress category page

I am in trouble with the category page in wordpress, I want to display the pagination and I used a method that works in other templates that I developed in the past.
I get the Category ID
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
Declare the pagination
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wpquery = new WP_Query(array(
'order' => 'DESC',
'cat' => $cat_id,
'posts_per_page' => 4,
'paged'=>$page
));
And after my Loop I show the pagination
global $wpquery;
if( $wpquery->max_num_pages >1){
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wpquery->max_num_pages
) );
}
If you try to go here
http://192.185.20.70/~t1t/tobeus.it/category/press/
you can see that the first & second page works, the third shows a 404...
in the other categories like "events"
http://192.185.20.70/~t1t/tobeus.it/category/events/
only the first page works...
I can't solve this strange problem!
I also had a problem with pagination in category template.
Try to change 'format' parameter 'paged' of paginate_links function to any other.
Example:
Set other parameter name, for example 'newparam'
echo paginate_links( array(
...
'format' => '?newparam=%#%',
Then get it from your url
$page = (get_query_var('newparam')) ? get_query_var('newparam') : 1;
Also don't forget to declare 'newparam' in functions.php file 'add_query_vars_filter' function.
Other solutions didn't worked for me, but this one did. I cannot explain exactly why, but it might be that parameter 'paged' is used somewhere when the second page is loading or something..
In this example you will just use other variable name to pass page number, and when the other pagination page will load, you will assign that value to 'paged' again, so it will load correct data from database.

Main loop with custom post type added - paginate_links doesn't work well

I have such a piece of code
<?php
global $wp_query;
$args = array_merge( $wp_query->query_vars, array( 'post_type' => array( 'post', 'project') ) );
$wp_query = new WP_Query( $args );
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
after the loop i have
<?php
$permalink_structure = get_option('permalink_structure');
$format = empty( $permalink_structure ) ? '?paged=%#%' : 'page/%#%/';
echo paginate_links( array(
'base' => get_pagenum_link(1) .'%_%',
'format' => $format,
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_text' => __('«'),
'next_text' => __('»'),
'show_all' => false,
'mid_size' => 2,
'end_size' => 1,
) );
?>
now the problem is that if I have 6 posts and 18 projects and 3 posts per page ... paginate_links will generate (6+18)/3 pages i.e. 8 pages ... so I click on 2 and go to page number 2 .. but when I click on 3 .. I get error 404.
As if paginate_links generates the required amound of page links but only the links to 6/3 pages word .. like 1 and 2.
Problem is for sure because of custom post type added but I can't understand where is that problem.
What may be the problem?
It looks like you have to alter the "main query" (you are using a "sub query" inside the "main query") to include your custom post type, so your pagination links will work.
You can try to alter the "main query" using the pre_get_posts hook
add_action( 'pre_get_posts', 'my_pre_get_posts' );
function my_pre_get_posts( $query ) {
if($query->is_main_query() && $query->is_home()){ // <-- EDIT this condition to your needs
$query->set( 'post_type', array( 'post','projects' ) );
}
}
where you place this code into the functions.php file in your current theme directory.
This assumes you are using the pagination on the frontpage, i.e.
http://example.com/page/5
We have the condition $query->is_home() to check if we are on the frontpage. If you are on a different page, you can alter this condition to your needs.
ps: I think your way is not working because you are doing it in the theme file and that is "too late" to alter the scope of the pagination links.

How to implement Wordpress pagination?

I'm trying to implement pagination for my posts. Though I'm a bit stuck on the php function and also how to call it.
The simple method is this I guess:
<?php posts_nav_link(); ?>
But what if I want custom pagination?
Here is my current code:
<?php
global $wp_query;
$total = $wp_query->max_num_pages;
if ( $total > 1 ) {
if ( !$current_page = get_query_var('paged') )
$current_page = 1;
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total,
'mid_size' => 4,
'type' => 'list'
));
}
?>
Is this correct and how do I call it? In index.php? Where in the loop? Thanks.
Edit for clarification: How do I implement this code? At the moment I have put it in my functions.php. So how (and where in the loop) do I 'reference' this function so the pagination is displayed.
There are two ways you can implement this code. It looks like right now you are sort of in between the two.
The first way would be to add your pagination code directly into the template that it will be used in somewhere inside the loop (most likely somewhere right before the closing <?php endwhile; ?> tag). If you are using a single.php template, you would put it in there, if not, put it in index.php. The placing of it inside of the loop depends on where you want the pagination to appear on your page.
The second way is to add the pagination code to the functions.php file (which you have done). However, you will need to revise your code a bit for this. You need to wrap the code within a function, and name that function something. I've used your_custom_pagination for an example. Your functions.php file is most likely already wrapped in php tags, so I have removed those.
function your_custom_pagination() {
global $wp_query;
$total = $wp_query->max_num_pages;
if ( $total > 1 ) {
if ( !$current_page = get_query_var('paged') ) {
$current_page = 1;
}
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total,
'mid_size' => 4,
'type' => 'list'
));
}
}
Then you'll need to go into the template that you are using and place this code <?php your_custom_pagination(); ?> into the same spot I illustrated above to call the pagination function.
I haven't actually tested your code, so assuming it's valid everything should work.

Resources