I've created a custom taxonomy => cat-blog in my custom post => blog, cat-blog have 4 terms and each terms have list of post belong to that term
Example of terms:
- City Updates (4 post belong)
- Home Tips (6 post belong)
- Real Estate Guide (8 post belong)
- Real Estate Industry (9 post belong)
and using this query
<?php
$query = new WP_Query(array('posts_per_page' => 2, 'post_type' => 'blog', 'blog-cat' => get_the_term_list( $post->ID, 'blog-cat' )));
while ($query->have_posts()) : $query->the_post();
?>
<?php
// content here
?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php
?>
to display 2 post in same category,
AND I just want to put next and prev pagination, so I can navigate the rest of the post, belong to that term.
Don't ever change the main query for a custom query on archive pages and on the home page. The main query already does what you want to do. Trying to run a custom query to try and get the same result is like reinventing the wheel. It also causes problems with pagination
SOLUTION
First, remove your custom query, and return to the main loop. The following is all you need in your taxonomy.php
if( have_posts() ) {
while( have_posts() ) {
the_post();
//REST OF YOUR LOOP
}
}
Use pre_get_posts in conjuction with the conditional tags if you need to alter the main query. For instance, if you need 2 posts per page on your taxonomy page, do the following in functions.php
function so26499451_custom_ppp( $query ) {
if ( !is_admin() && $query->is_tax() && $query->is_main_query() ) {
$query->set( 'posts_per_page', '2' );
}
}
add_action( 'pre_get_posts', 'so26499451_custom_ppp' );
You can now paginate as normal without any problems. You will now see two posts from the specific term that you clicked on per page on your taxonomy.php.
Related
I want to display in my WP search results ONLY pages with specific template beceuse I trying to build product catalog without any plugins. Pure WP code. Some pages are my products, and they have product.php template file. This is my search.php:
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/post/content', 'excerpt' );
endwhile;
the_posts_pagination();
else :
?>
<p><?php _e( 'No results.' ); ?></p>
<?php
get_search_form();
endif;
?>
And the question is, how to display only my product pages without any other pages?
Try this out!
This function will interrupt the search query and add some condition fo the query to returns specific pages.
//Filter the search for only posts and parts
function SearchFilter($query)
{
// Now filter the posts
if ($query->is_main_query() & $query->is_search && $search_template) {
$query->set('post_type', 'page');
$query->set('meta_key' => '_wp_page_template',);
$query->set('meta_value' => 'product.php');
}
// Returning the query after it has been modified
return $query;
}
add_action('pre_get_posts', 'SearchFilter');
Explaining the code:
The "post_type" will limit this filter for page post type only.
The condition Will limit this filter to only work on the search queries and not every time the posts query is called.
$query->is_search && $search_template
These parameters will filter the returned posts by the meta_key "_wp_page_template" which contains the page template, so we only return the pages with the page template "product.php".
$query->set('meta_key' => '_wp_page_template',);
$query->set('meta_value' => 'product.php');
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.
In category.php I am using custom query to get posts:
<?php
$cat_id = get_query_var('cat');
$args = array(
'posts_per_page' => 2,
'orderby' => 'date',
'cat' => $cat_id
);
query_posts($args);
// the Loop
get_template_part('aa_HomeLoopMain');
?>
I am using get_query_var('cat') to get category post of current category and I think this would only give Category Posts of category id with $cat_id but not it's child category posts?
You are doing it wrong. Never ever use query_posts, it breaks the main query object, reruns queries and is slow which all negatively impacts on performance and SEO and other functions that relies on the main query. Also, if this is your main query, you should not be using a custom query at all, you should be using pre_get_posts to alter the main query before it executes.
get_query_var(cat) only returns the queried category, not its children.
You should remove the query_posts part and add the following to your functions.php
add_action( 'pre_get_posts', function ( $q )
{
if ( !is_admin()
&& $q->is_main_query()
&& $q->is_category()
) {
$q->set( 'posts_per_page', 6 );
}
});
EDIT
You should query a total of 6 posts, I have updated my as such. You can try the following with your loop
if ( have_posts() ) {
while( have_posts() ) {
the_post();
if ( 1 <= $wp_query->current_post ) {
// Add your markup for column one, this will display 2 posts
} else {
// Add your markup for column two, this will display 4 posts
}
}
}
EDIT 2
For some reason I cannot post comments from my mobile, but I think you are using the code wrongly. I have updated my code to show the loop. It does work. If it does not, something else is breaking your page like query_posts
I am developing locally on MAMP using "Custom Post Types" and "Custom Fields" plugins. I have a custom post type of "Products" and a custom taxonomy of "Types."
I'm trying to use my pagination in "Taxonomy.php" because that's where I'm going to display my custom post types of "products" with my custom taxonomy "types" and use conditionals to generate different content on that same taxonomy.php.
This is the code in taxonomy.php that I got from css-tricks:
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=3&post_type=products'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
// my content from my custom field that I want paginated
<p><?php the_field('image'); ?></p>
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
This question has been asked a lot around the web and I have read at least 30 different articles and blogs about this and still can't seem to find a solution. With this method of pagination I got from css-tricks the only problem is I keep getting a 404 error message whenever I try to go to the next page unless I set my "Blog pages show at most" to 1 in settings > reading. If I do that then it works, otherwise it gives me the 404 page. My permalinks are set to Post Name, by the way.
So I can see my pagination here:
http://localhost/MyWebsite/types/cars/
but when I get here I get the 404 page.
http://localhost/MyWebsite/types/cars/page/2/
I have read about the Flush Method but that does not work. I only have 2 plugins activated (custom post types and fields) and those are not the problem. How can I fix the 404 error?
I have diagnosed the problem further and I figured out whats going on. This is now my code in terminology.php:
<?php
$args = array(
'post_type' => array( 'products' ),
'posts_per_page' => 5,
'paged' => ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1
);
$products_query = new WP_Query( $args );
$temp = $wp_query;
$wp_query = null;
$wp_query = $products_query;
while ($products_query->have_posts()) : $products_query->the_post();
?>
// content to display
<p><?php the_field('image'); ?></p>
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
Bear with me because this is a little bit confusing. I'll try my best to clearly explain the difference between "Blog pages show at most" in settings > reading and my posts_per_page arguments.
I have a category of Honda with a sub-term of Accord and I have 3 products of my custom post type assigned to Accord
Now if I changed my posts_per_page argument what that does is no matter how many "Products" I have under "Accord" it will always demonstrate that amount even if you do not have enough.
So if I set it to 5 and I have 3 products it will still show 5 either repeated or from another category. When I click on the next pagination the pages will only go as far as however many items that I have. That is if "Blog pages show at most" is set to 1, which brings me to my next point.
Now the difference in changing "Blog pages show at most" is that even if I have my posts per pages set to 5 and I have only 3 "Products" but I change my "Blog pages show at most" to 3 it will not go to the next page because I only have 3 "products" in accord term.
If I were to change my "Blog pages show at most" to 1, I would be able to go forward 3 pages because I have 3 items.
It's sort of like changing "Blog pages show at most" changes the core of WordPress so no matter how many you put to display in the posts_per_page args it will only really count however many you set in "Blog pages show at most".
It seems like the pagination count is based upon "Blog pages show at most", not posts_per_page. I can have posts per page set to 10 and "Blog pages show at most" set to 2 and it would show 10 but only count as if there were really 2 on each page meaning I would only be able to go to the 2nd page of my "Accord" term that has 10 displaying in both of them.
Get the pattern? WordPress counted 2 products per page even though it was displaying 10. So it told the pagination that it can't go past page 2 because I only have 3 products.
But if you have a custom post type products and a taxonomy types you will show your content on: archive-products.php and taxonomy-types.php you could use pre_get_posts action to modify the query if you need and then write the markup you want on those files, example:
Put this on your functions.php
<?php
add_action( 'pre_get_posts', 'mr_modify_archive_taxonomy_query' );
function mr_modify_archive_taxonomy_query( $query ) {
if ( !is_admin() && $query->is_main_query() ) {
if ( is_post_type_archive( 'products' ) || is_tax( 'types' ) ) {
$query->set( 'posts_per_page', 3 );
}
}
}
?>
an use a default loop
<?php
if ( have_posts() ):
while ( have_posts() ): the_post();
printf( '<p>%s</p>', get_the_field( 'image' ) );
endwhile;
previous_posts_link('« Newer');
next_posts_link('Older »');
endif;
?>
I am trying to create a series of pages that display the posts within a single category. To do this I use the following PHP code:
<?php
$args = array( 'category' => '$CATEGORY', 'numberposts' => 10000000000);
$myposts = get_posts( $args );
foreach($myposts as $post) :
setup_postdata($post);
?>
My problem is that the $CATEGORY does not seem to contain the category as a string. I have tried using both %s and $id but as I have not declared that it is the category id I am wanting it fails to work. The resulting output has been either an error or all the posts regardless of category.
What argument will convey the category string?
Below is a page illustrating the problem. This is a category page, meaning it should hold all the necessary info. If it was working it would only show the topmost post as it is the only post en site that has the "Press Release" category. Worth mentioning that I have another page just like it called "Dokument" and it displays the press release.
Page: http://www.skinwellness.se/category/pressrelease/
EDIT
I did not notice this before, but it seems that you are using the bundled theme twentythirteen. Simply delete the category.php from your child theme. If you have made changes to the parent theme directly, you should get a fresh copy of the theme, and create a child theme with all your modifications. Never make changes to a theme that you did not write. When such themes update, all you changes will be gone forever
You should then just need the pre_get_posts section in your child theme's functions.php to make everyone work
ORIGINAL ANSWER
You problem is purely your custom loop. As explained in the linked post, you should not be using custom queries in place of the main query on any type of archive page or on your home page
To solve this, revert back to the default loop. This is all you should have. No get_posts or foreach loops
if( have_posts() ) {
while( have_posts() ) {
the_post();
// Add your loop elements here like the_title() and the_content()
}
}
This should fix the problem that when you visit a category page, only the category been viewed will be viewed, no posts from other categories will be shown
Now, if you need to change anything on your category page, use pre_get_posts to do that. Make use of the is_category() conditional tag to target your category pages only. You can also target a specific category with this tag
Say for instance, you need to change the posts per page on you category page, as your case, display all posts with no pagination, you can do this in your functions.php
function so26589648_category_ppp( $query ) {
if ( !is_admin() && $query->is_category() && $query->is_main_query() ) {
$query->set( 'posts_per_page', '-1' );
}
}
add_action( 'pre_get_posts', 'so26589648_category_ppp' );
If you need to for example change the order to ASC and need to order posts by author, you can do this
function so26589648_category_ppp( $query ) {
if ( !is_admin() && $query->is_category() && $query->is_main_query() ) {
$query->set( 'posts_per_page', '-1' );
$query->set( 'order', 'ASC' );
$query->set( 'orderby', 'author' );
}
}
add_action( 'pre_get_posts', 'so26589648_category_ppp' );
You should see WP_Query for all available parameters to use