Support » Fixing WordPress » Remove sticky posts from "posts page"

  • I want to remove sticky posts from my posts page.
    I’m using a child theme of twenty-eleven and the home page (called “Base”) uses the showcase template to display featured posts.

    The post page (called “Ticker”) uses the sidebar template. I want to remove the featured posts from the top of this page (while retaining them on the showcase template of the home page)

    The site:
    http://www.jrsfantasticmachines.com/

    Thanks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • If you are using the sidebar-page.php template that comes with T11, make a copy of sidebar-page.php to your child theme’s folder and add the following code just before the ‘while ( have_posts() )’ line:

    <?php
    $stickies = get_option('sticky_posts');
    if( $stickies ) {
       $args = array( 'ignore_sticky_posts' => 1, 'post__not_in' => $stickies );
       global $wp_query;
       query_posts( array_merge($wp_query->query, $args) );
    }
    ?>
    Thread Starter diagrammar

    (@diagrammar)

    Thanks, copied sidebar-page.php and added code but no change to the “Ticker” page.

    Do i need to put something into the Child Theme stylesheet that tells it to use sidebar-page.php from the child theme folder?

    The sidebar-page.php in my child theme folder now begins like this:

    get_header(); ?>
    
    		<div id="primary">
    			<div id="content" role="main">
    <?php
    $stickies = get_option('sticky_posts');
    if( $stickies ) {
       $args = array( 'ignore_sticky_posts' => 1, 'post__not_in' => $stickies );
       global $wp_query;
       query_posts( array_merge($wp_query->query, $args) );
    }
    ?>
    
    				<?php while ( have_posts() ) : the_post(); ?>

    No, that should do it. To be sure that you are using the correct template, change the get_header() line to this:

    get_header(); ?><!-- file:sidebar-page.php in child theme -->

    Then view the page, look at the source, and search for file:.

    First: if you’re using the static page called “Ticker” for your blog posts index, then that page is not using the sidebar-page.php template file. Per the Template Hierarchy, the Blog Posts index uses, in descending order, only one of the following template files:

    1. home.php
    2. index.php

    So, since Twenty Eleven doesn’t include a home.php, the page is actually being rendered using index.php.

    That’s really more just FYI, because the solution I propose doesn’t care what template file is being used. I would, instead of using query_posts(), filter pre_get_posts instead:

    function mychildtheme_filter_pre_get_posts( $query ) {
        if ( is_page( get_option( 'page_for_posts' ) ) ) {
            $query->set( 'ignore_sticky_posts', true );
        }
        return $query;
    }
    add_filter( 'pre_get_posts', 'mychildtheme_filter_pre_get_posts' );

    Add that to your child Theme’s functions.php file. It tells WordPress: “if this page is the page used to display the blog posts index, then modify the query to ignore sticky posts.”

    The great benefit of this method (in addition to being, generally, cleaner) is that the query is modified before posts are queried – meaning that everything – including pagination – will work entirely as expected.

    Hey Chip,

    Any particular reason you opted to check if it’s the page for posts rather than the homepage?

    This works perfect for me.

    function mychildtheme_filter_pre_get_posts( $query ) {
        if ( is_home() ) {
            $query->set( 'ignore_sticky_posts', true );
        }
        return $query;
    }
    add_filter( 'pre_get_posts', 'mychildtheme_filter_pre_get_posts' );

    Any particular reason you opted to check if it’s the page for posts rather than the homepage?

    Yes: because I didn’t want to assume that you wanted to modify handling of sticky posts if you later decided to display the blog posts index on the site front page.

    As an alternative, you could test for the showcase template itself:

    function mychildtheme_filter_pre_get_posts( $query ) {
        if ( ! is_page_template( 'showcase.php' ) ) {
            $query->set( 'ignore_sticky_posts', true );
        }
        return $query;
    }
    add_filter( 'pre_get_posts', 'mychildtheme_filter_pre_get_posts' );

    That way, you would only ever see/use sticky posts on a page using the Showcase template, regardless of what other template file is loaded.

    Thread Starter diagrammar

    (@diagrammar)

    Thanks Chip,

    tried that. it’s much simpler. but still no dice.

    The page I call “Ticker” is chosen as the “posts page” in the Reading Settings of WordPress. When I edit the “Ticker” page, it lists the Template as “Sidebar template”

    How would I alter the code you provided to specify the ignore-sticky-posts command for a single page? I want featured (sticky) posts ignored only on the page called “Ticker.” They can work normally on all other pages.

    Thread Starter diagrammar

    (@diagrammar)

    Thanks done…

    Used Chip’s second suggestion and simply tested for sidebar.php rather than showcase.php

    Cheers very helpful, was looking for exactly Chip’s solution.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Remove sticky posts from "posts page"’ is closed to new replies.