I have set my wordpress blog's frontpage to show only three posts.
On the archive.php template, when the posts of a tag are viewed, I want to show 10 results.
How do I do this?
I tried this php code. But instead of only showing the posts with a certain tag, it queries all recent posts.
//in archive.php (before the loop)
query_posts('posts_per_page=10');
Just append the query with a tag parameter (as shown here: http://codex.wordpress.org/Function_Reference/query_posts#Tag_Parameters):
query_posts('posts_per_page=10&tag=your_desired_tag');
EDIT: If you use this within a function you can also just append your limit to the original query like this:
function my_archive_loop($content) {
global $query_string;
query_posts($query_string . "&posts_per_page=10");
}
The variable $query_string should than include all the default parameters like the current tag, category, year or whatever archive page you are viewing.
can you try this:
function post_per_page_control( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
// For archive.You can omit this
if ( is_archive() ) {
//control the numbers of post displayed/listed (eg here 10)
$query->set( 'posts_per_page', 10 );
return;
}
// For your tag
if ( is_tag() ) {
//control the numbers of post displayed/listed (eg here 10)
$query->set( 'posts_per_page', 10 );
return;
}
}
add_action( 'pre_get_posts', 'post_per_page_control' );
read more here:
1 http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
2 http://codex.wordpress.org/Function_Reference/query_posts
Related
I would like:
http://www.gadgetgogo.co.uk/?s=ipod
to return as:
http://www.gadgetgogo.co.uk/?s=ipod&post_type=product
So when using searches (slider banner and default WordPress search) it produces the second URL.
I was looking for similar solution as you but couldn't find any and at last I combined last few answers that I read here and got this working fine as I wanted.
Add below code in your theme's functions.php file
function wpb_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) && ($_GET['post_type'] != 'product') ) {
wp_redirect( home_url( "/?s=" ) . urlencode( get_query_var( 's' ) ) . "&post_type=product" );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );
hope it will help you and others.
Apparently current WooCommerce versions only consider a search query a product search (and render using the appropriate template), if $query->is_post_type_archive( 'product' ) is true, so the key is to set not only the post_type, but the is_post_type_archive property as well, and to do it before WooCommerce loads its filter (default priority of 10), so with a priority of 9 or smaller.
Example to add into funtions.php:
function my_search_filter($query) {
if ( $query->is_search && ! is_admin() ) {
$query->set( 'post_type', 'product' );
$query->is_post_type_archive = true;
}
}
add_filter('pre_get_posts','my_search_filter', 9);
Please note, that this code will override all searches as product serach, so if you have other searches as well, implement appropriate checks at the begining of my_search_filter.
Just add this line to top of search.php
$_GET['post_type'] = 'product'
This can be done using pre_get_posts filter. Add below code in your theme's functions.php file
add_filter( 'pre_get_posts', 'search_by_product_only' );
function search_by_product_only( $query ) {
// check if search query only
if ( $query->is_search ) {
$query->set( 'post_type', array( 'product') ); // here you can add multiple post types in whcih you want to search
}
return $query;
}
Currently by default Wordpress display 8 posts in category.php page, i need to increase or decrease limit.
How to get it?
Thank you.
Use the pre_get_posts filter to modify the number of posts displayed on categories.
Example:
function wpse_modify_category_posts_per_page( $query ) {
// Check we're on the frontend and modifying the main query.
if ( ! is_admin() && $query->is_main_query() ) {
// Change to 8 posts per page when viewing a category.
if ( $query->is_category() ) {
$query->set( 'posts_per_page', 8 );
}
}
}
add_action( 'pre_get_posts', 'wpse_modify_category_posts_per_page' );
Here we begin by checking we're not in the admin and we're affecting the main query. Then we test whether we're viewing a category page.
If all of those tests are passed we use the set method to change the number of posts per page.
Further reading: http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
In your category.php file, before the while loop, add this
// Get the last 10 posts in the special_cat category.
<?php query_posts( 'posts_per_page=10' ); ?>
Replace 10 with your desired number.
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
I have a template where there is a main latest featured post (tagged as featured), and then 8 more below it.
On the homepage, i was able to query for the latest featured post, and then pass its id to a function in the pre_get_posts wordpress filter. it works great there
function mh_exclude_featured_query( $query ) {
if(is_home()){
if ( $query->is_home() ) {
$feat = get_featured_post();
$query->query_vars['post__not_in'] = array($feat->ID);
}
}
}
add_action( 'pre_get_posts', 'mh_exclude_featured_query' );
but i'm also trying to do the same thing in the category.php, where i would show the latest post tagged as featured from that category. and then the remaining posts below with the featured post excluded.
Unfortnately, when i try the same method as above by using the pre_get_posts filter, i get stuck in an infinite loop and run out of memory.
if($query->is_category() && $query->is_main_query()){
$cur_cat_id = get_cat_id( single_cat_title("",false) );
$feat = get_featured_post($cur_cat_id);
$query->query_vars['post__not_in'] = array($feat->ID);
}
not sure what i'm doing differently that leads to a memory exhaustion. the category.php and index.php are near identical in their structure.
use the pre_get_posts filter:
<?php
function excludePostId($query) {
$postIds = array(
24, 10
);
if (is_category() && is_main_query()) {
set_query_var('post__not_in', $postIds);
}
}
add_action('pre_get_posts', 'excludePostId');
is_category() will accept a category slug or ID. You can limit which categories the post will be excluded from.
Add This Code
<?php if(!is_category('category_id')){
echo 'Your Code Here';
}?>
How can I hide posts from some category with some ID from main page of my site? I need solution like filter:
function exclude_post($query) {
if ($query->is_home) {
...
}
return $query;
}
add_filter('pre_get_posts', 'exclude_post');
Can somebody provide an example?
Use $query->set( $query_var, $value ); where $query_var is the variable you want to add/update in query. So put this inside your condition:
// 1st parameter is the query variable the 2nd is its value, in this case an array of category IDs
$query->set( 'category__not_in', array( 2, 6 ) );
Remember is good practice put in condition a check to $query->is_main_query(). pre_get_posts is an action hook so you have to change add_filter to add_action.
An action hook doesn't return a value, a filter does.
Example
function exclude_post( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'category__not_in', array( 2, 6 ) );
}
}
add_action('pre_get_posts', 'exclude_post');
UPDATE
According to the new details emerging by the question,in order to exclude some posts in feeds stream, but not in category archive, the conditional check might look like:
if( $query->is_feed() && !$query->is_archive() )
OR
if( $query->is_feed() && !$query->is_category() )
Hope it helps!
you can also use the following way to exclude the a category from post query
<?php
if ( is_home() )
{
query_posts($query_string . '&cat=-3');
}
?>
You can use simple exclude in the query parameters :-
<?php wp_list_categories('orderby=name&show_count=1&exclude=10'); ?>
Its only sample , how to you exclude.
Hop it work..