i have a custom post type and would like to change the number of posts per page. i use the following code to show/limit the posts:
$args = array(
'post_type' => 'movie',
'posts_per_page' => 3,
'tax_query' => array(
array(
'taxonomy' => $wp_query->queried_object->taxonomy,
'field' => 'slug',
'terms' => $wp_query->queried_object->slug,
)
),
'paged' => get_query_var('paged') ? get_query_var('paged') : 1
);
query_posts( $args );
for this time i have 20 posts with this post type and the default posts per page in wordpress admin (settings/read ...) is set to 10.
if i call the url without a page it shows 3 posts, if i call the url with "page/2/" it shows 3 posts but if i call the page with "page/3/" it shows nothing found ...
i wouldn't change the default value for the posts in the admin - does someone have a idea?
note: to "debug" i let me show the value for "$wp_query->post_count;" - this use the default admin posts per page - 10 ...
regards kai
When you load the page, it will do a query before the template file (archive.php) is called.
That first query will use the defaults set in WordPress; ie. 10 posts per page. By the sounds of things, you're running that second 'query_posts' call within archive.php, but it sounds like that is after you have run the appropriate checks.
Two possible solutions for you:
Modify the first query with the posts_per_page
add_filter( 'pre_get_posts', 'modify_movie_query' );
function modify_movie_query( $wp_query ) {
if( $wp_query->query_vars['post_type'] != 'movie' ) return;
$wp_query->query_vars['posts_per_page'] = 3;
}
Because you have modified a reference to $wp_query, you don't need to return it.
Run the query_posts call at the top of the archive.php template
Option 1 is what I would recommend - it's the most efficient, and the most appropriate.
The approved answer doesn't really work if the archive page is for a custom taxonomy. In that case, you might want to use the following:
add_filter( 'pre_get_posts', 'modify_movie_query' );
function modify_movie_query( $wp_query ) {
if(is_tax('movie-category')){
$wp_query->query_vars['posts_per_page'] = 10;
}
return $wp_query
}
Related
I have a custom theme in WordPress and I just created a custom pagination for a custom query.
Once I click page 2 or higher, it will redirect me to [URL]/[URI]/page/2, yet in my screen it will only show the code in index.php.
Somehow this URI with this new parameter (page) is loading the wrong file in my WordPress theme. How can I solve this?
This is my query:
//Parametro para saber cuantos posts abran por pagina!
$post_per_page = 6;
//Extracción de la pagina actual
$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
// set up or arguments for our custom query
$query_args = array(
'post_type' => 'recetas_practicerdo',
'category_name' => 'dia_a_dia',
'order' => 'ASC',
'posts_per_page' => $post_per_page,
'paged' => $paged
);
// create a new instance of WP_Query
$the_query = new WP_Query( $query_args );
Pagination is working good and it echoes the right content. Every button in my <ul><li> elements are correct.
Yet the result is the wrong file. =(
After sesarching for a while I decided to use the get_template_part( ) function in wordpress.
I grab my URI and created some logical rules. If they were true, load the template from where my pagination should be redirecting.
I know it's not the best solution but didn't find any answer on the web.
Some answers said that pagination with custom wp_queries should break some plugins (which is my case).
Also, after I load the right template, I had to also create a head-title for every redirect I did.
If someone knows how to really fix this problem, I will be grateful with the help.
I am having trouble with Wordpress pagination. I have a custom archive page displaying custom post types from the specific category. I want to use pagination and display 12 posts per page. My problem is that pagination works correctly but only up to the 8th page. After that I am being presented with "Page not found".
I am using theme built-in function to display page navigation (1, 2... 10, 11). It correctly shows 11 pages in total, but they seem not to work after the 8th page.
$taxonomy = 'product_cat';
$term_id = get_queried_object()->term_id;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'product',
'paged' => $paged,
'posts_per_page' => '12',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $term_id
)
)
);
<?php $wp_query = new WP_query( $args ); ?>
<?php if( $wp_query->have_posts() ): ?>
<?php while( $wp_query->have_posts() ): ?>
<?php $wp_query->the_post(); ?>
//post content
<?php endwhile; ?>
<?php endif; ?>
<?php s7upf_paging_nav();?>
<?php wp_reset_postdata(); ?>
#edit
When I go to a different category page which should have 12 pages they work correctly up to the 9th page.
If I go to the one that has 2 pages, only the first one works.
I tried updaing permalinks. Setting posts per page to 12 in wordpress settings.
When i change posts per page in my query args to -1 it correctly shows all the posts on one page.
When manually setting the number of a page to display ('paged' => '11') it also displays correct page with correct posts.
You are using the wrong query. On page you are creating your own query instead of the actual query the page already did.
This will also be better for performance.
Step 1. Check what is in the normal query.
At the top of taxonomy-product_cat.php
global $wp_query;
var_dump( $wp_query->query_vars );
That probably fits mostly.
Step 2. Do the normal loop
Remove all your query stuff (maybe keep a backup of the $args for the next step)
example: Replace
<?php if( $wp_query->have_posts() ): ?>
with
<?php if( have_posts() ): ?>
And so on.
Step 3. Edit the main query
We are going to use the hook pre_get_posts
add_action('pre_get_posts', 'so_53315648');
function so_53315648( WP_Query $wp_query ){
// only check this on the main query
if (! $wp_query->is_main_query() ){
return;
}
// only check this on the product_cat taxonomy
if ( ! $wp_query->is_tax('product_cat')) {
return;
}
// maybe do some more checks?
$wp_query->query_vars['posts_per_page'] = 12;
// Is this really needed??
//$wp_query->query_vars['posts_type'] = 'product';
// tweak the query the way you like.
}
As you can see $wp_query->query_vars should pretty much be the same $args. But do not overwrite it. This might break other stuff.
Of course I could not test your specific site. But the answer should be inside the pre_get_posts hook. And tweaking the main query instead of doing a extra separate one.
Test, also check the var_dump of step 1 that your changes are coming through.
The checks at the top are to stop other pages from being affected, maybe you need more?
Let me know.
function when_a_review_gets_submitted ($review, $post_id) {
// Do something
}
add_action('rwp_after_saving_review', 'when_a_review_gets_submitted', 11, 2);
I have this WP_Query and loop to update a custom field of every childpage of the parentpage were somebody has left a review.
// Set up the objects needed
$hosting_provider_query = new WP_Query();
global $post;
$hosting_pagina_titel = $post->post_title;
$search_all_pages = $hosting_provider_query->query(array(
'post_type' => 'page',
'post_status' => 'publish',
//Only get pages where custom field 'name_hosting_provider' equals Page title
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'naam_hosting_provivder',
'value' => $hosting_pagina_titel,
'compare' => '='
),
),
));
// Loop through all pages and find Page's children
$loop_through_all_child_pages = get_page_children( get_the_ID(), $search_all_pages );
// Loop through everything we got back
if(!empty($loop_through_all_child_pages)){
foreach($loop_through_all_child_pages as $child_page){
// get the ID of each childpage
$get_child_page_ID = $child_page->ID;
// get the ID of each parent page of the child pages
$get_parent_page_ID = get_queried_object_id();
// Get the average score from post_meta of the parent page
$get_average_score = get_post_meta( $get_parent_page_ID, 'rwp_user_score', true );
// update each custom field of the childs with the average score data from the post meta of parent page
update_field('gemiddelde_score_hosting_provider', $get_average_score, $get_child_page_ID);
}
}
How can I correctly execute this code within the above function?
EDIT
Ok I figured this out, the function is working properly when I remove the parameters $review and $post_id. And when I remove the add action:
add_action('rwp_after_saving_review', 'when_a_review_gets_submitted', 11, 2);
and replace it with:
add_action( 'template_redirect', 'when_a_review_gets_submitted' );
but I think this is a bit overkill for what I am trying to achieve. If I understand template_redirect it executes the function everytime a page is visited with that template? Am I correct?
So when I have 900 child pages it updates every custom field "gemiddelde_score_hosting_provider" when a page with that template is visited? So 900 times every single time a page with that template is visited? Am I correct in this one?
Is there a way to only update the custom field of the childs from the parent page where the review is submitted?
I want to have a static front page on my WordPress and this static one should display the latest 5 posts and below that up to 6 custom meta fields. Thats why I changed the reading settings from last posts to "Static page", because otherwise I would not have the possibility to access the custom meta fields.
How can I now include the latest 5 blogposts on my front-page and use the default post template/output.
I did it with this query:
$latest_blog_posts = new WP_Query( array( 'posts_per_page' => 5 ) );
if ( $latest_blog_posts->have_posts() ) : while ( $latest_blog_posts->have_posts() ) : $latest_blog_posts->the_post();
// Loop output goes here
endwhile; endif;
Do i now have to rewrite the code for the post here completly or can I just include the post template in some way?
Thanks!
You need to write this line to show latest posts:
<?php
wp_get_archives(array('type' => 'postbypost', 'limit' => 10, 'format' => 'html'));
?>
Check this link for more details
Instead of WP_Query you should try
wp_get_recent_posts( $args, $output );
Check more details here
My Wordpress site has multiple custom categories and posts types all which need to be displayed on the home page blogroll. But I need the first post on the homepage always to be the most recent post from a specific category.
For example the pst at the top of the page will always be the latest "giraffe" category; and below it; sorted default/chronologically are more "walrus" mixed with "seagull" and "nachos"
What would be the best way to accomplish this?
Without running through the Loop twice, sticky posts are your best option.
Otherwise:
...
$args = array(
'orderby' => 'post_date',
'category'=> 'giraffe',
'numberposts'=> 1,
);
$latest_giraffe_post = get_posts( $args );
...
then do what you're already doing for the rest