15

We all know that ignore_sticky_posts is used to exclude sticky post from your custom query. - No, this assumption is wrong. What ignore_sticky_posts means: Even though in natural English, ignore_sticky_posts sounds like WordPress should ignore all sticky posts from the query, in reality that is not what WordPress does. Instead, you should read '...


12

Here is an approach to account for sticky posts by getting the number of sticky posts (if any) and include that in the calculation posts_per_page parameter: add_action('pre_get_posts', 'ad_custom_query'); function ad_custom_query($query) { if ($query->is_main_query() && is_home()) { // set the number of posts per page $...


10

The sticky posts are saved as an array of post IDs in the wp_options table. Hence, $stickies = get_option( 'sticky_posts' ); $stickies[] = $post_id; update_option( 'sticky_posts', $stickies ); will make the post in question sticky. EDIT: Even better, the core provides functions to stick and unstick posts (had to have 'em). stick_post( $post_id ); ...


9

Digging through the source code I couldn't really find any weird logic that would cause 'category__in' to break the ordering of the results. If you do, you might as well file a bug in the WordPress Trac. It's also hard to reproduce this sort of issue, because it might depend on another issue which is specific to your database or how the data was previously ...


7

There are a few functions that come in handy here: unstick_post - Unstick a post stick_post - Stick a post is_sticky - Figure out if a post is sticky With those three in mind, all we need to do is stick them together with some admin menu bar glue. First off, let's wrap everything in a class for fun and profit. This class will have some constants that we'...


6

"Admin Stickies" for custom post types: To support sticky custom posts in the backend, you can use the following code snippet in your functions.php file in the current theme directory or in your custom plugin: /** * Set admin stickies for the 'foodmenu' custom post type */ add_action( 'init', function() { if( function_exists( 'wpse_cpt_stickies' ) ) ...


6

I currently have no posts set as stickies on the website. Which tells me that nothing should show up in the loop. Exactly where you are wrong when passing an empty array to post__in. WordPress has some silly bugs which has no proper workaround and will most probably stay active bugs for a very long time. This is one of them. When we pass a valid array of ...


5

query_posts isn't recommended because its breaks things. You're really close, but just declaring the function itself will not work. You need to hook the function into something. In your case, this would be pre_get_posts. Example (with "namespaced" function): <?php // this is key! add_action('pre_get_posts', 'wpse74620_ignore_sticky'); // the function ...


5

This is doable, and as I stated in a comment, you need to follow the following steps Get a count the amount of sticky posts. This can be done by simply counting get_option( 'sticky_posts' ) which holds the ID's of the sticky posts in the form of an array You would also need to get the amount of posts per page. You don't want to hardcode here. The amount of ...


5

Sticky posts are a feature of the home page, not of the post. The post has no reason to "know" it is sticky. The home page OTOH do not have any formal form of storage and therefor traditionally options related to it are stored in the options table


4

The problem is that you misuse the category field. Use a site meta field (array) and a simple checkbox on each post. When a post is saved add its ID to the site meta array, and if the array is longer than 5 drop the last ID from it. The query for featured post will then just take the post IDs from the meta field and you will never have to uncheck something ...


4

If I'm understanding you correctly, you want to show only the most recent sticky post only on the front page. I had the same issue a month or two ago, and got some fantastic help from the community here at WordPress Answers. The solution is to run two loops in your index.php file. One to pull only the most recent sticky post, and the second to display all ...


4

I think this small source code is your solution. It currently doesn't have frontend feedback for the Sticky Post change, but you can enhance this string, class or whatever you want in the function fb_stick_post. The first function adds the item in the Admin Bar and creates a new Url with a param value. Also, on click calls a function to read the Url param ...


4

Don't use query_posts. Use a filter on pre_get_posts. function no_front_sticky_wpse_98680($qry) { if (is_front_page()) { $qry->set('post__not_in',get_option( 'sticky_posts' )); } } add_action('pre_get_posts','no_front_sticky_wpse_98680'); By running query_posts you clobber the main query, over-writing it with another query. That is why you break ...


4

By default, sticky posts only stick to the top of the first page of the main blog posts index. The easiest way to show sticky posts in other contexts is probably via a custom loop, e.g.: $sticky_posts = new WP_Query( array( 'post__in' => get_option( 'sticky_posts' ) ) ); if ( $sticky_posts->have_posts() ) : while ( $sticky_posts->have_posts() ...


4

If you want to totally remove the sticky posts from the query, you need to use post__not_in. The Codex has an example, which you can adapt to your needs: $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; $sticky = get_option( 'sticky_posts' ); $args = array( 'cat' => 3, 'ignore_sticky_posts' => 1, 'post__not_in' => $...


4

You have custom filter in a plugin or in your theme that is messing with WP_Query. Your issue is that something is setting is_home to false. This is not default behavior, by default, is_home should return true in any custom instance of WP_Query. is_home is one of the contributing factors which is responsible for setting up sticky posts at the top in a ...


4

If you look ate the source code where stickies are included, we find the following condition before WP_Query carries on to include sticky posts if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) { The big game player is the ...


4

Here's a way to force the exact posts_per_page value in WP_Query, regardless of sticky posts or custom post injects: $args = [ 'posts_per_page' => 1, '_exact_posts_per_page' => true // <-- our custom input argument ]; by using our custom _exact_posts_per_page input argument. I'm sure this has been implemented many times before, ...


3

Here is a much simpler solution that will get the job done using the_content filter hook add_filter('the_content','simplest_sticky_solution'); function simplest_sticky_solution($content){ global $post; //early exit if not needed if (!is_single() || !current_user_can('edit_post',$post->ID)) return $content; //check if form is ...


3

$GLOBALS['wp_query']->found_posts will give you the number of posts. $GLOBALS['wp_query']->posts is an array with all the posts found. So instead of while ( have_posts() ) : the_post(); use: setup_postdata( $GLOBALS['wp_query']->posts[0] ); get_template_part( 'content', 'super' ); This way you don’t run through all post, you really just use one. ...


3

It doesn't appear that there is any obvious way to enable "sticky post" support for Pages, e.g. via add_post_type_support(). However, Pages support both hierarchy and menu order. I would recommend using menu order to output Pages in your preferred order.


3

As noted in the trac ticket, you need to add that UI element in yourself. If you're still looking for it, I made a plugin to add custom post type sticky support, available here.


3

For me, using ignore_sticky_posts keeps sticky posts from showing at the top, but they still show in chronological order with the other posts. I'm using post__not_in with get_option('sticky_posts') to exclude sticky posts from the main query. <?php /** * Exclude sticky posts from home page. */ function theme_name_ignore_sticky_posts($query){ if (...


3

There an issue if the sticky posts are in the first page. The solution is to decrement the sticky post count for the sticky posts that are part of the first page. function fix_posts_per_page_with_sticky_posts( $query ) { if ( $query->is_main_query() ) { // set the number of posts per page $posts_per_page = 12; // get ...


3

Use is_sticky( $post_id ) to test if a post is a sticky post: if ( is_sticky() ) echo 'I am sticky!'; if ( is_sticky( 123 ) ) echo 'The post with the ID 123 is sticky!';


3

The post parent can be accessed by $post->post_parent. The ID of the post parent is retrieved, so can try something like this $parent_ID = $post->post_parent; if( is_sticky( $parent_ID ) ) { // DO SOMETHING IF POST PARENT IS STICKY }


3

The issue is that you are using the function query_posts this only queries the default result on that page. It's advised that you use wp_query instead, it just eliminates the margin for error or unexpected results. You can create a new query like below, and specify explicitly how many posts to return: <?php $sticky = get_option( 'sticky_posts' ); rsort(...


3

This same exact question was asked earlier this week or over the weekend, and it had me thinking. Here is the idea that I came up with. If you look at the source code of the WP_Query class, you will see that sticky posts is only added to the first page of the home page. There is also no filter supplied to change this behavior in order to set the required ...


3

To make this more complete, here is what I have said in comments in reply to the question on hand Just to quickly explain, WP_Query fails catastrophically in some cases where empty arrays are passed to some of its parameters, instead of also returning an empty array as we should expect, WP_Query returns all posts. As for getting the correct stickies, as I ...


Only top voted, non community-wiki answers of a minimum length are eligible