开发者

Wordpress Sticky Posts with Custom Post Types

开发者 https://www.devze.com 2023-04-09 15:18 出处:网络
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

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 pre开发者_C百科dicament 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?


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.


You can use this plugin, it has it's own limitations, but works pretty well if you don't need something elaborate.


You can save a custom meta with the name of "sticky" and add it the value "on" when the post is sticky. That can be done with a custom metabox and a checkbox.

Wordpress will automatically add the word "Sticky" on the backend posts listing table

You can retrieve a loop with your sticky custom posts by adding the values 'meta_key' => 'sticky' and 'meta_value' => 'on' to the args of your query


I posted a working solution as of WordPress 4.2 here:

https://wordpress.stackexchange.com/questions/90958/adding-sticky-functionality-to-custom-post-type-archives/185915#185915

Basically, it implies installing a small plugin and add a code snippet.


I have Wordpress 3.2.1, the latest version and I can sticky posts. It works for me on my site.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号