WordPress pagination 404 error - wordpress

i am using WordPress paginate_links() function in my custom template but the problem is when ever i go to page 2 the page stucks and it gives 404 error in console. My custom query is
if( get_query_var( 'paged' )){
$paged_documents = get_query_var( 'paged' );
}
else if( get_query_var( 'page' )){
$paged_documents = get_query_var( 'page' );
}
else{
$paged_documents = 1;
}
$query = new WP_Query( array('post_type' => 'documents', 'posts_per_page' => 8,'paged'=>$paged_documents) );
My permalink is set to Post name.
I have already found two solutions.
1) Change permalink settings to "Plain". Which i dont want to.
2) Page name and url must be different from custom post type. For Example in above snippet post_type is document as well as page name and url. I dont want to change the page name and urls.

Try this:
https://developer.wordpress.org/reference/functions/get_query_var/
<?php
wp_reset_query();
$paged_documents = get_query_var('paged', 1);
$query = array(
'post_type' => 'documents',
'posts_per_page' => '8',
'paged' => $paged_documents
);
?>

Related

Wordpress batch for move posts status for custom type

Is there any way for change all custom posts type status change from "publish" to Draft ?
Scenario:
Here I need to run a script.
my post type is: property_listing
Now I need to make all post draft then perform my custom operation.
Is it possible ?
If you have Cpanel or database access you can run this query in DB panel.
update table wp_posts set status='draft' where post_type='property_listing'
otherwise, use this code.( paste it in your theme function.php)
add_action('init','change_mypost_status');
function change_mypost_status(){
$args = array(
'post_type' => 'property_listing',
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$current_post = array(
'ID' => get_the_ID(),
'post_status' => 'draft'
);
// Update the post into the database
wp_update_post( $current_post );
}
wp_reset_postdata();
}
}

The Wordpress Search.php pagination returns a 404 on page 2, when only 1 result is left

I'm working on a search.php page in Wordpress, and have used the following code to alter the query:
if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
elseif ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
else { $paged = 1; }
$args = array(
's' => get_search_query(true),
'showposts' => '8',
'paged' => $paged
);
$results = new WP_Query( $args );
It works fine when there are over 10 results, but if there are 9 results, when you click to the second page, it gives a 404 error.
I've tried resetting the permalinks, but no dice. The permalink link is:
site.com/page/2/?s=prep+production
Is there something I'm missing to get the that 9th one to show up?

Wordpress wp_query and custom fields

I have a category, where posts are listed by this wp_query:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'stage' => 'process',
'paged' => $paged,
);
$query = new WP_Query( $args );
Using plugin m_snap (alphabetical order of posts) there is a problem. It provides custom field for any post with a letter. So, the URL becomes something like this:
[...]/category/product/letter/A/ but has no difference with [...]/category/product/
How can I correct wp_query to get proper list of posts using m_snap?
UPD. Found that it's something wrong with rewriting urls. This is code from plugin:
function m_snap_generate_rewrite_rules($wp_rewrite) {
$IIIIIIIII1ll = array (
'letter/(.+)' => 'index.php?letter=' . $wp_rewrite->preg_index(1),
'tag/(.+?)/letter/(.+)' => 'index.php?tag='.$wp_rewrite->preg_index(1).'&letter='.$wp_rewrite->preg_index(2),
'category/(.+?)/letter/(.+)' => 'index.php?category_name='.$wp_rewrite->preg_index(1).'&letter='.$wp_rewrite->preg_index(2)
);
$wp_rewrite->rules = $IIIIIIIII1ll + $wp_rewrite->rules;
}
...
add_action ( 'generate_rewrite_rules', 'm_snap_generate_rewrite_rules' );
Any ideas to correct?

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.

Wordpress custom post type pagination by $_GET param

I have a single page where I want to make a pagination for a certain custom post type called 'article' and I'm also using a $_GET param called 'week'.
Basically when I go to http://www.mydomain.com/single-page/?week=7 I want to see the pagination of my posts from 'article' post type with taxonomy called week equal to 7.
All it's fine, but paginate_links function doesn't act how I want. For example the 2 pagination link has this url http://www.mydomain.com/single-page/?week=7/page/2. But this url doesn't work, it gives an 404 Error.
But this url http://www.mydomain.com/single-page/page/2/?week=7 it's working. So what is the solution?
Is there a way to tell Wordpress to use www.mydomain.com/single-page/page/2/?week=7 when I go to www.mydomain.com/single-page/?week=7/page/2 ?
Do you have some good tutorial for custom posts type pagination?
This is the code I'm using to paginate:
//The paginate function
function paginate_articles_week()
{
global $wp_query, $wp_rewrite;
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
$pagination = array(
'base' => #add_query_arg('page'),
'format' => '?page=%#%',
'total' => $wp_query->max_num_pages,
'current' => $current,
'show_all' => true,
'type' => 'list',
'prev_text' => '«',
'next_text' => '»',
);
if ($wp_rewrite->using_permalinks())
$pagination['base'] = user_trailingslashit(trailingslashit(remove_query_arg('s', get_pagenum_link(1))) . 'page/%#%/', 'paged');
if (!empty($wp_query) && $pagination['total'] != 1) {
$pagination['add_args'] = array('s' => get_query_var('s'));
echo "<div class='paginate'><strong>Pagini:</strong>" . paginate_links($pagination) . "</div>";
}
}
//The page template
global $post;
$week = $_GET['week'];
$slug = 'week-' . $week . '-month-2';
$paged = 1;
$postsPerPage = 1;
if (get_query_var('paged')) { $paged = get_query_var('paged'); }
if (get_query_var('page')) { $paged = get_query_var('page'); }
$args = array(
'post_type'=> 'article',
'paged' => $paged,
'posts_per_page' => $postsPerPage,
'timeline' => $slug
);
query_posts($args);
{the loop}
paginate_articles_week();
Thanks in advance for the answers :)

Resources