• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Programming / WordPress Genesis Loop Only Display Sticky Posts

WordPress Genesis Loop Only Display Sticky Posts

Last updated on September 28, 2015 by Sal Ferrarello


Warning: DOMDocument::loadHTML(): Tag template invalid in Entity, line: 12 in /home/customer/www/salferrarello.com/public_html/wp-content/plugins/gistpress/includes/class-gistpress.php on line 466

Warning: DOMDocument::loadHTML(): Tag svg invalid in Entity, line: 14 in /home/customer/www/salferrarello.com/public_html/wp-content/plugins/gistpress/includes/class-gistpress.php on line 466

Warning: DOMDocument::loadHTML(): Tag path invalid in Entity, line: 15 in /home/customer/www/salferrarello.com/public_html/wp-content/plugins/gistpress/includes/class-gistpress.php on line 466

Warning: DOMDocument::loadHTML(): Tag template invalid in Entity, line: 27 in /home/customer/www/salferrarello.com/public_html/wp-content/plugins/gistpress/includes/class-gistpress.php on line 466

Warning: DOMDocument::loadHTML(): Tag svg invalid in Entity, line: 29 in /home/customer/www/salferrarello.com/public_html/wp-content/plugins/gistpress/includes/class-gistpress.php on line 466

Warning: DOMDocument::loadHTML(): Tag path invalid in Entity, line: 30 in /home/customer/www/salferrarello.com/public_html/wp-content/plugins/gistpress/includes/class-gistpress.php on line 466
<?php
// add the following code to your functions.php file
add_action( 'pre_get_posts', 'fe_pre_get_posts_sticky_only' );
function fe_pre_get_posts_sticky_only( $query ) {
if (
$query->is_main_query()
&& $query->is_home()
) {
$query->set( 'post__in', get_option('sticky_posts') );
}
}
view raw functions-snippet-sticky-posts-only.php hosted with ❤ by GitHub

Update 2015-09-28 Thanks to Joan in the comments for pointing out the original code posted here was only relevant when using a Genesis Custom Loop. This updated solution applies to the default loop (and is not Genesis specific).

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit

Filed Under: Programming Tagged With: Genesis, WordPress

Reader Interactions

Comments

  1. Joan says

    September 26, 2015 at 12:38 am

    Hi Sal! I’m trying this code but it has no effect on the loop. Tried in functions.php, in my own plugi, even in home.php and front-page.php, but it has no effect.

    Am I missing something?

    Reply
    • Sal Ferrarello says

      September 26, 2015 at 6:08 am

      I think you’re right Joan. The current code should only work if you’re using a Custom Genesis Loop. I think using the pre_get_posts hook would be the better way to go. I’ll take a look closer and update this post. Thanks for pointing this out.

      For right now, I think putting the following code into `functions.php` should do the trick

      
      
      add_action( 'pre_get_posts', 'fe_pre_get_posts_sticky_only' );
      function fe_pre_get_posts_sticky_only( $query ) {
          if (
              $query->is_main_query()
              && $query->is_home()
          ) {
              $query->set( 'post__in', get_option('sticky_posts') );
          }
      }
      Reply
  2. Joan says

    September 26, 2015 at 6:51 am

    Yeah, now it works great.

    So yeah, “genesis_custom_loop_args” only works if you are using “genesis_custom_loop”, which makes sense. 😉

    Thanks, Sal! Appreciate!

    Reply
  3. Birgit says

    December 16, 2015 at 4:25 pm

    There might be a use case, why you only want sticky posts. Have you handled a category page where the sticky posts 2 or more are on top and the other post are order by publication date?
    I have look around the Interwebs, but couldn’t find this use case.

    Reply
    • Sal Ferrarello says

      December 16, 2015 at 5:11 pm

      Hi Birgit,

      I don’t do a lot with Sticky posts, which is why I wrote this post to remind myself in the future of how to do this. If you find a solution to your issue, I’d be interested in hearing about it. Best of luck.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in