Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress Sticky Posts with Custom Post Types

Tags:

wordpress

So i need the ability to have a featured or "sticky" post in wordpress, and it occurred to me! Why not use the Sticky Posts facility, but after doing a bit of reading it seems that Wordpress decided to not include support for it in latest releases and they don't seem to be pushing any solution for future releases.

Now that leaves me in a predicament i wish to have the ability to have a featured post or custom post without using a category of such.

I've also seen a few people state they have hacked wordpress with possibly a function to add the ability of sticky posts to custom post types, shame they didn't share the source!

How would this be done?

like image 203
Xavier Avatar asked Nov 27 '22 16:11

Xavier


1 Answers

You can do it with a custom field (post_meta) on the custom post type. Then fire a custom query that selects for the meta_value:

$args = array('post_type' => 'my_custom_post_type', 'post_status' => 'publish', 'meta_query' => array('relation' => 'AND', array('key' => 'is_sticky', 'value' => '1', 'compare' => '=', 'type' => 'CHAR')));

$sticky_posts = new WP_Query($args);

Should return an array of published posts of post_type: my_custom_post_type that have the sticky flag set.

Although I haven't tested the above code, I'm doing something similar and it works fine.

like image 162
Rob Maurizi Avatar answered Dec 24 '22 01:12

Rob Maurizi