{"id":485,"date":"2012-08-29T09:18:38","date_gmt":"2012-08-29T06:18:38","guid":{"rendered":"https:\/\/polyetilen.lt\/?p=485"},"modified":"2023-04-20T14:01:27","modified_gmt":"2023-04-20T11:01:27","slug":"show-only-sticky-posts-on-home-page-in-wordpress","status":"publish","type":"post","link":"https:\/\/polyetilen.lt\/en\/show-only-sticky-posts-on-home-page-in-wordpress","title":{"rendered":"Show only sticky posts on home page in WordPress"},"content":{"rendered":"<p>To create a list of posts by WordPress query it is possible to filter it by adding a custom filter to the condition. For example, if you need to show only sticky posts, you have to take a list of all sticky posts and add optional filter to WHERE condition.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$sticky = get_option('sticky_posts');\r\n$where .= ' AND wp_posts.ID IN ('.implode(',', $sticky).') ';\r\n<\/pre>\n<p>If such a condition is necessary only for the main page, you have to add a check is_home(). To change the conditions in the query in WordPress there is a special filter posts_where, which returns the current value of the WHERE condition of the database query. This filter can be used to develop plugins, for example to show only sticky content on the main page:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\/\/Plugin Name: Forever Sticky\r\n\/\/Description: Show only sticky posts on home page\r\n\/\/Author:      Polyetilen\r\n\/\/Version:     1.0\r\n\r\nfunction forever_sticky_condition ( $where ) { \r\n\tif(is_home()){\r\n\t\t$sticky = get_option('sticky_posts');\r\n\t\tif(count($sticky)&gt;0){\r\n\t\t\treturn $where.' AND wp_posts.ID IN ('.implode(',', $sticky).') ';\r\n\t\t}else{\r\n\t\t\treturn $where;\r\n\t\t}\r\n\t}else{\r\n\t\treturn $where;\r\n\t}\r\n}\r\n\r\nadd_filter('posts_where', 'forever_sticky_condition'); \r\n?&gt;\r\n<\/pre>\n<p>Download plugin <a href=\"https:\/\/polyetilen.lt\/wp-content\/uploads\/2012\/08\/forever_sticky.7z\">Forever sticky<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To create a list of posts by WordPress query it is possible to filter it by adding a custom filter to the condition. For example, if you need to show only sticky posts, you have &hellip; <a href=\"https:\/\/polyetilen.lt\/en\/show-only-sticky-posts-on-home-page-in-wordpress\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"en_US","_original_post":"http:\/\/polyetilen.lt\/?p=289","footnotes":""},"categories":[8,88],"tags":[117,163,115],"class_list":["post-485","post","type-post","status-publish","format-standard","hentry","category-programavimas","category-wordpress","tag-filtras","tag-lipnus-irasas","tag-wordpress-2","en-US"],"_links":{"self":[{"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/posts\/485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/comments?post=485"}],"version-history":[{"count":2,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/posts\/485\/revisions"}],"predecessor-version":[{"id":487,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/posts\/485\/revisions\/487"}],"wp:attachment":[{"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/media?parent=485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/categories?post=485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/polyetilen.lt\/wp-json\/wp\/v2\/tags?post=485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}