How to retrieve the sticky posts for a sort

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPrint this page

The default WordPress Sticky functionality is very limited. It allows to put a post on top of the list which is pretty much everything about it. Beside other missing functionality, there is no control over order, making the list totally uncontrollable.

The Advanced Post Types Order plugin provides a similar internal Sticky Posts, which is very flexible and preferable to be used instead default.

Sticky Posts are adjusted/applyed automatically on front side, on a query run. Programmatically retrieval for those posts is also easy, the following code can be used:

    $attr = array(
                        '_view_selection'   =>  'taxonomy',
                        '_taxonomy'         =>  $taxonomy_name,
                        '_term_id'          =>  $term_id
                        );
    
    $sort_view_id   =   $APTO->functions->get_sort_view_id_by_attributes($sort_id, $attr);
    $sticky_list = get_post_meta( $sort_view_id , '_sticky_data', TRUE);

or for archive type

    $attr = array(
                        '_view_selection'   =>  'archive'
                        );
    
    $sort_view_id   =   $APTO->functions->get_sort_view_id_by_attributes($sort_id, $attr);
    $sticky_list = get_post_meta( $sort_view_id , '_sticky_data', TRUE);

The returned $sticky_list is an array containing the list of sticky posts. The key of array element is the position within the list and the value is the object_id.