Support » Plugins » Hacks » Use posts_orderby filter and custom fields to do a sticky category post

  • Hello,

    I want to use the add_filter('posts_orderby', 'myPostOrder'); to implement a sticky post function.
    There are a few plugins out there, but they don’t really work. For example WP Category Sticky Posts. If I use this plugin in combination with The Events Calendar sticky posts are listed as well in the widget of “the Events Calendar”, because “Wp Category Sticky Posts” uses the “the_posts” filter.

    Hence I want to write my own function. Therefore I want to use a custom field named “sticky-Post”. This custom field is only set “true” if the post is a sticky post. If the post isn’t a sticky post no custom field will be set.
    I don’t know how the $orderby should look like in this case. How do I check if a post has the custom field “sticky-Post” and make it the first one to appear in a special category? And furthermore how to sort the other posts by date?

    I hope someone can help me!

    Thanks in advance :)!


Viewing 1 replies (of 1 total)
  • Moderator bcworkz


    You can’t use that filter alone to alter the query because custom fields are stored in a different table. You will need to join the postmeta table into the query to orderby custom fields.

    First determine how you would write a direct SQL query to get the results you desire. Then look at how the various query filters would play into forming your desired SQL query by examining code in wp-includes/query.php, starting around line 2567. The query is actually formed on line 2612, starting with $this->request = .... You will then see how you need to set each filter to get what you want.

    You orderby multiple columns just by listing the columns in order, separated by commas.

Viewing 1 replies (of 1 total)
  • The topic ‘Use posts_orderby filter and custom fields to do a sticky category post’ is closed to new replies.