Skip to content

Instantly share code, notes, and snippets.

@srikat
Created July 19, 2015 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srikat/5e0796fb3ce0dbcca047 to your computer and use it in GitHub Desktop.
Save srikat/5e0796fb3ce0dbcca047 to your computer and use it in GitHub Desktop.
How to limit Posts on Posts page to a specific Category in WordPress. https://sridharkatakam.com/how-to-limit-posts-on-posts-page-to-a-specific-category-in-wordpress/
add_action( 'pre_get_posts', 'sk_show_posts_from_a_category_posts_page' );
/**
* Show Posts from a specific category on Posts page
*
* @author Bill Erickson
* @author Sridhar Katakam
* @link http://www.billerickson.net/customize-the-wordpress-query/
* @param object $query data
*
*/
function sk_show_posts_from_a_category_posts_page( $query ) {
if( $query->is_main_query() && !is_admin() && is_home() ) {
$query->set( 'category_name', 'category-1' ); // Replace "category-1" with your category slug
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment