Categories
Free Tutorials Genesis Tutorials

Show Sticky Posts Only On Front Page

Assuming your home page reading settings are configured to display your latest posts in a paginated archive rather than a static page, you can use this PHP code in your child themes functions file to display your sticky posts on only.

If there’s no sticky posts, the default loop of posts are displayed.

To modify the loop on other archive page types, change the is_home conditional tag in the above code.

If your front page uses a front-page.php template with widget areas, you can use the Genesis Featured Posts widget settings to display only sticky posts.

Related Tutorials

Categories
Free Tutorials WordPress Tips

Only Show Specific Pages or Posts On Home Page

This code enables you to select which single pages you want displayed on the front page of your WordPress site. It excludes all posts and only shows pages.

Single Page by I.D #

The following code displays a single page with the ID of 1594 on your home page.

add_filter( 'pre_get_posts', 'display_page_only_in_loop' );

function display_page_only_in_loop( $query ) {

if ( ! is_admin() && is_home() && $query->is_main_query() ) {
  
  $query->set('post_type', array( 'page' ) );
  
  $query->set('post__in', array( '1594' ) );
  
  }
  
}

Single Post by I.D #

The following code displays a single post with the ID of 68356 on your home page.

add_filter( 'pre_get_posts', 'display_post_only_in_loop' );

function display_post_only_in_loop( $query ) {

if ( ! is_admin() && is_home() && $query->is_main_query() ) {
  
  $query->set('post__in', array( '68356' ) );
  
  }
  
}

Multiple Single Pages by I.D #

To display multiple single pages on your home page, use a comma separated array of page id’s like this :

add_filter( 'pre_get_posts', 'include_pages_in_loop' );

function include_pages_in_loop( $query ) {

if ( ! is_admin() && is_home() && $query->is_main_query() ) {

    $query->set('post_type', array( 'page' ) );
  
    $query->set('post__in', array( 1594, 852 ) );
  
  }
  
}

Swap out the page id’s in the above code from 1594, 852 to your own page ID’s.

Multiple Single Posts by I.D #

To display multiple single posts on your home page, use a comma separated array of post id’s like this :

add_filter( 'pre_get_posts', 'include_posts_in_loop' );

function include_posts_in_loop( $query ) {

if ( ! is_admin() && is_home() && $query->is_main_query() ) {
  
    $query->set('post__in', array( 1, 2 ) );
  
  }
  
}

Swap out the post id’s in the above code from 1, 2 to your own post ID’s.

Multiple Single Pages & Posts by I.D #

To display multiple single posts & single pages on your home page, use a comma separated array of post and page id’s like this :

add_filter( 'pre_get_posts', 'include_pages_posts_in_loop' );

function include_pages_posts_in_loop( $query ) {

if ( ! is_admin() && is_home() && $query->is_main_query() ) {

    $query->set('post_type', array( 'post', 'page' ) );
  
    $query->set('post__in', array( 1594, 852, 68356, 68353 ) );
  
  }
  
}

Swap out the post & page id’s in the above code from 1594, 852, 68356, 68353 to your own post ID’s.

Categories
Free Tutorials Genesis Tutorials

Include Single Pages With Posts In Home Page Loop

This code laters the main query for the home page loop to include single pages with posts.

Add the PHP code to the end of your child themes functions.php file

add_filter( 'pre_get_posts', 'include_pages_in_loop' );

function include_pages_in_loop( $query ) {

  if ( ! is_admin() && is_home() && $query->is_main_query() ) {
  
  $query->set('post_type', array( 'post', 'page' ) );
  
  }
  
}

Adds the page post type to the original query before it runs so single pages are included with single posts in the home page loop.

Related Tutorials

Categories
Free Tutorials

Set Posts Per Page For Any Archive Type

Use this code in your child themes functions file with a conditional tag to target a specific archive page type to set posts per page.

add_action( 'pre_get_posts', 'set_posts_per_archive_page' );
 
function set_posts_per_archive_page( $query ) {
 
if ( ! is_admin() && $query->is_main_query() ) {
 
    $query->set('posts_per_page', '5');
 
    }
}

The above code sets all archive pages to display 5 posts per page.

Post Per Page For Specific Category #

Only executes on the news category archive page.

add_action( 'pre_get_posts', 'set_posts_per_archive_page' );
 
function set_posts_per_archive_page( $query ) {
 
if ( ! is_admin() && $query->is_main_query() && is_category( 'news' ) ) {
 
    $query->set('posts_per_page', '1');
 
    }
}

Swap out the slug news in the above code to target a different category.

Swap out the 1 to change the number of posts per page you want to display on the archive page.

Categories
Free Tutorials WordPress Tips

Return Search Results After A Specific Date

If you want to exclude older posts from your search results, you can using pre_get_posts.

The following code uses the date_query from WP_Query.

Only returns results after the 1/1/2013 using the WordPress search function.

add_filter( 'pre_get_posts', 'search_filter' );
function search_filter($query) {

if ( ! is_admin() && $query->is_main_query() ) {
    if ($query->is_search) {

$query->set( 'date_query', 
    [    [
    'after'     => 'January 1st, 2013',
    'inclusive' => true,
    ]    ] 
    );
    }
  }
}

The code works with the WordPress search function and search widget included in all WordPress installations.

Categories
Free Tutorials Genesis Tutorials

Add Custom Field To Genesis Loop

This code enables you to add custom field content like text, HTML & images to the Genesis loop. You can use the code 2 ways:

  1. The 1st snippet of code can be added directly to any template file which uses the WordPress Template Hierarchy. See 1st snippet below which includes the code for adding custom fields content to a archive loop.
  2. You can also use the code in your functions file with any conditional tag added after the function name. See 2nd code snippet below.
  3. And you can create a custom archive template file and only output specific content in replace of the default content displayed on standard archives

The code also works when using the Genesis Portfolio Pro plugin to generate a CPT named portfolio.

1st Snippet

Use the WordPress Template Hierarchy to name your template file so its loaded on specific archive page types like category, tag, author, custom post type & taxonomy archive page types.

Using a code editor, create a new file in your child themes root directory and add the code below to the file.

2nd Snippet

For use in genesis child themes functions file with conditional tag

ACF Check

When using a plugin for your custom field input, make sure you include a check to make sure the plugin is active like this :

if ( ! class_exists( 'acf' ) ) 
    return;

Pagination

Pagination is set using the default Reading settings for posts per page. You can override these settings using code in your functions file like this:

add_action('pre_get_posts', 'posts_per_cpt_archive_page');

function posts_per_cpt_archive_page( $query ) {

if ( ! is_post_type_archive( 'portfolio' ) )
    return;

$query->set( 'posts_per_page', 1 );
    return;
}

The above code sets the posts per page at 1 for the portfolio archive. Swap out the conditional tag is_post_type_archive to target your archive page type.

Custom Field Archive Template File

Name your file using the WordPress Template Hierarchy and add the following code.

Demo Video – Category Archive Loop

The demo video shows how HTML for a image has been added to the value field for a custom field named key. Swap out key with the name of your custom field.

The image has been added to the category archive loop for the category named demo.

You can also use this solution to add custom field content to a custom post type loop.

Demo Video – CPT Archive Loop

The video shows you how to add custom field content a custom post type archive loop named portfolio. It also explains how to display pagination and override the default pagination settings using pre_get_posts.

Learn more : Beginners guide to using custom fields.

Related Tutorials

Categories
Genesis Tutorials

Genesis Search Results Template You Can Fully Customize

This template for Genesis, enables you to customize how the search results are displayed on the search results page. The template includes settings in the code which you can easily modify to fully customize the output on the frontend so its different to the standard search results.

The template enables you to:

  • Set the layout
  • Show or remove the featured image
  • Set the featured image size using any default WordPress > Media or added custom image sizes
  • Align the featured image left, right, center or none
  • Display the full content, set content character limit or display the excerpts
  • Customize the read more link for each entry
  • Set posts per page for each result

Template Installation

  • Upload the search.php template to your child theme folder.
  • Add the Optional Code below to your child themes functions.php file.

Here’s the template and code for logged in members:

Related Tutorials

Categories
Genesis Tutorials

Minimum Pro – Change Portfolio Archive Entries To 3 or 4 Columns

By default, the Minimum Pro child theme by StudioPress is coded to display the portfolio archive entries in 2 columns like this:

This tutorial shows you how to change the columns from 2 to 3, 4 or 6.

Here’s the demo video showing the portfolio archive page displaying 3 columns with pagination and the portfolio taxonomy type archive displaying posts in 4 columns:

You can also limit the number of posts per page for both the portfolio archive page and the portfolio taxonomy type archive page so they display a different amount of entries to whats set in the WordPress > Reading settings.

The solution in this tutorial relates to the use of the:

1. archive-portfolio.php file and the
2. taxonomy-portfolio-type.php file.

Here’s the code for registered users:

Categories
WordPress Tips

Set Posts Per Page For Tag Archive

This code enables you to set the posts per page limit for any tag archive page. This way you can over ride the WordPress > Reading settings which effect all archive page types.

The following code only executes on the tag archive page with a slug tag-one. Replace tag-one with the slug or i.d of your tag.

add_action('pre_get_posts', 'posts_per_tag_archive_page');

function posts_per_tag_archive_page( $query ){
if ( $query->is_tag('tag-one') ) {
    $query->set( 'posts_per_page', 1 );
    return;
    }
}

The following code effects all tag archive pages:

add_action('pre_get_posts', 'posts_per_tag_archive_page');

function posts_per_tag_archive_page( $query ){
if ( $query->is_tag() ) {
    $query->set( 'posts_per_page', 1 );
    }
}

This enables you to display pagination on your tag archives.

The code uses the is_tag conditional tag with pre_get_posts.

Categories
Genesis Tutorials

Exclude From Genesis Blog Page Template

This code enables you to exclude single posts & pages as well as posts in different types of archives like author, category, search, and tag archive type pages.

Simply add to your child themes functions file and swap out the i.d to suit your needs.

The code determines if the blog template ( included in Genesis ) is being loaded. You’ve probably tried using is_home(), is_page('blog') and is_page_template('page_blog.php') with pre_get_posts however those conditionals will not work with the blog page template.

Here’s the code for logged in members:

Categories
WordPress Tips

Set Different Amount of Posts Per Page For Each Archive

This code enables you to set a different amount of posts per archive page, otherwise use the default WordPress Reading settings.

The example snippet in this tutorial sets a specific amount of posts per page for any number of categories, otherwise it uses the posts per page limit set in your WordPress Reading settings.

On top of this, it also includes a specific limit for search results pages and the home page.

To use this code, simply swap out the conditional tags for any type of archive and use your own I.D’s for each archive type.

Note for Genesis users: To set the posts per page for any pages using the Blog Page template, use the Genesis > Theme Settings > Blog Page Template settings as seen in the following image:

genesis-blog-page-template

Here’s the code for logged in members, you can simply paste in your functions file. Works in any theme.

Categories
Free Tutorials WordPress Tips

Exclude Latest Post From Home Page Loop

In this tutorial. i’ll provide the code i tested which excludes the latest published post from your home page loop.

Simply copy this PHP from the view raw link in the Gist and paste it at the end of your child themes functions.php file using a code editor.

The code above simply removes your most recent published post from the home page loop and can also be easily modified to offset any number of posts from the posts page you select in your Reading settings.

The home page loop may also be called your posts page or blog page.

When you publish a new post, it will be removed from the loop until you publish another post.

Similar Tutorials & Code Snippets

Categories
Free Tutorials WordPress Tips

3 Ways To Remove or Exclude Posts From Your Home Page Loop

There’s at least 3 ways you can exclude one or more single posts from your home or posts page depending on your Reading Settings.

  1. You can use CSS code
  2. You can use PHP code
  3. You can use a plugin

CSS Code

The easiest way to remove a specific post from your home page is to use CSS code in your child themes srtyle.css file.

.home .post-37401 {
display: none;
}

Simply grab the post i.d from your source code and replace it in the code above.

PHP Code

Another method is to use the posts i.d’s in a custom function in your child themes functions.php file:

add_action( 'pre_get_posts', 'wpsites_remove_posts_from_home_page' );
function wpsites_remove_posts_from_home_page( $query ) {

    if( $query->is_main_query() && $query->is_home() ) {
        $query->set( 'post__not_in', array( 37401, 37403 ) );
    }
}

Simply replace the post i.d’s in the code above with your own.

Plugin

If you looking for a non coding option, there’s several plugins which get the job done, one of which is Simple Exclude.

Related Solutions

Categories
Free Tutorials Genesis Tutorials

Add Custom Post Type (CPT) Pages To Posts Page

By default, only posts are displayed on your posts page which can either be your home page or blog page depending on your Reading Settings.

Reading Settings

If your theme includes custom post types (CPT), you can also add the single pages assigned to your CPT to your posts page as well.

Simply add this PHP code to the end of your child themes functions.php file and change the name portfolio to the name of your CPT.

add_action( 'pre_get_posts', 'add_custom_post_types_to_loop' );

function add_custom_post_types_to_loop( $query ) {
	if ( is_home() && $query->is_main_query() )
		$query->set( 'post_type', array( 'post', 'portfolio' ) );
	return $query;
}

You can add multiple CPT’s to this array like this.

add_action( 'pre_get_posts', 'add_custom_post_types_to_loop' );

function add_custom_post_types_to_loop( $query ) {
	if ( is_home() && $query->is_main_query() )
		$query->set( 'post_type', array( 'post', 'portfolio', 'video' ) );
	return $query;
}

Any custom taxonomy types you add to your CPT will automatically be included in your loop so you don’t need to add them in the array.

You can also add static pages to your posts page if you really want to simply by adding ‘page’ to the array in the above code snippets.

Add Custom Post Type to Archive Page

Change the is_archive() conditional tag to any other like is_category().

add_action( 'pre_get_posts', 'add_custom_post_types_archive' );

function add_custom_post_types_archive( $query ) {
	if ( $query->is_main_query() && !is_admin() && is_archive() )
		$query->set( 'post_type', array( 'post', 'your-cpt' ) );
	return $query;
}

Simply change your-cpt to the name of your custom post type.

More Code Using Pre Get Posts

Categories
Genesis Tutorials

Customize Archive Pages Conditionally Using Pre Get Posts

In this tutorial, i’ll give you several PHP code snippets i have tested locally which enable you to customize how your posts are displayed on any archive.

Modify this code at the end of your child themes functions.php file according to your own needs.

A reader asked: I need to change the order of posts being displayed on a category archive page. I need them to be oldest first.

Note: These code snippets will work in any WordPress theme.

Custom Post Type Archive Page

This code display 6 posts per page from a custom post type named portfolio with the most recent post displayed first.

add_action( 'pre_get_posts', 'custom_post_type_archive' );

function custom_post_type_archive( $query ) {

if( $query->is_main_query() && !is_admin() && is_post_type_archive( 'portfolio' ) ) {

		$query->set( 'posts_per_page', '6' );
		$query->set( 'orderby', 'title' );
                $query->set( 'order', 'DESC' );
	}

}

Category Archive Page

This PHP code displays the oldest post first and only 1 post per archive page.

function conditional_custom_category_limit( $query ) {

    if ( is_admin() || ! $query->is_main_query() )
        return;

    if ( is_archive(category-i.d) ) {
        
                $query->set( 'posts_per_page', 1 );
		$query->set( 'orderby', 'title' );
                $query->set( 'order', 'ASC' );
        return;
    }

  
}
add_action( 'pre_get_posts', 'conditional_custom_category_limit', 1 );

Simply replace the category-i.d with your actual category i.d or category permalink slug.

Limit Posts Per Category Archive Page

This line in the code above enables you to control how many posts display on any archive page:

$query->set( 'posts_per_page', 1 );

Simply change the 1 to any number you like.

Order Newest or Oldest Posts First

This line in the code above enables you to control which posts are displayed first. You can use ASC or DESC for newest or oldest posts first:

$query->set( 'order', 'ASC' );

Simply change the ASC to DESC and vice versa.

Source http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

Related Solutions

Categories
Free Tutorials Genesis Tutorials

3 Ways To Display Posts In One Category On Front Page of Genesis

This is a beginners guide to setting up what posts display on the front page of your site.

If you only want to display posts from one category on your front page, please follow these simple steps:

If that’s the case, you can:

  1. Create a new page
  2. Select the blog page template from the drop down menu under Page Attributes
  3. Publish your new blog page
  4. Go to Settings > Reading and under > Front page > a static page > select the blog page as your front page.

front page displays

Then go to Genesis > Theme Settings > Blog Page Template > Display which category > Select the category of posts you want to display.

blog page template

You can also choose to displays posts from all categories and then exclude the i.d’s for the categories you don’t want to show on your home page.

Create Page Template For One Category

You can also create a very basic custom blog page template using code like this.

Custom Function

Another option is to use a custom function in your child themes functions file:

function home_one_category( $query ) {
    if ( $query->is_home() AND $query->is_main_query() AND ( ! is_admin() ) ) {
        $query->set( 'cat', 'your category I.D Here' ); 
}
}
add_action( 'pre_get_posts', 'home_one_category' );

Simply replace your category I.D Here in the code above with your category I.D.

Other Options