How to get bbpress sticky topics

I want to get bbpress sticky topics using query like this:

$sticky = get_option('sticky_posts');
    rsort( $sticky );
    $sticky = array_slice( $sticky, 0, 2);
        query_posts( array( 'post__in' = $sticky, 'caller_get_posts' = 1, 'post_type'= 'topic' ) );
if (have_posts()) : 
    while (have_posts()) : the_post(); 
//do something
endwhile;
endif; 

How can i make query something like this work?

Topic theme-thesis bbpress sticky-post Wordpress

Category Web


Another answer, from this bbpress.org thread

$query = new WP_Query(array('post__in' => bbp_get_super_stickies());


For future reference, if you only want to get an array of the super sticky topics (stuck to front), then you can simply use this code:

$stickies = get_option( '_bbp_super_sticky_topics', array() );

a better way to to this:

$query = new WP_Query( array( 'p' => $post->ID, 'post_type'=> 'forum', 'meta_key' => '_bbp_sticky_topics' ) );
            if ( $query->have_posts()) : 
                while ($query->have_posts()) : $query->the_post(); 
                    $sticky = get_post_meta( $post->ID, '_bbp_sticky_topics' );
                    $sticky = maybe_unserialize( $sticky );                 
                endwhile;
            endif;          
            wp_reset_postdata();

Then $sticky will be an array with all of the sticky topics from the current forum you are querying, you could do a second query with post__in to show the sticky topic's content, title, etc.


After lots of workaround i found the solution. This code will give you the results and why i will explain:

<?php 
$query = new WP_Query( array( 'posts_per_page' => 2,'post_type'=> 'forum', 'meta_key' => '_bbp_sticky_topics' ) );
if ( $query->have_posts()) : 
    while ($query->have_posts()) : $query->the_post(); 
        the_title(); echo '<br />'; the_ID(); echo '<br />';
    endwhile;
endif; 
/* Restore original Post Data */
wp_reset_postdata();
?> 

the trick is to use 'post_type'=> 'forum' instead of 'post_type'=> 'topics', and it will show you the forums instead of topics because wp_postmeta save data like this a:1:{i:0;i:632;} with the post_id of forum not topics. Thanks for Vinod Dalvi, he really gave me a direction to solve this issue.


You can make the query like above using following code.

<?php
query_posts( array( 'posts_per_page' => 2, 'meta_key' => '_bbp_sticky_topics',  'post_type'=> 'topic', 'order' => 'ASC' ) );
if (have_posts()) : 
    while (have_posts()) : the_post(); 
        //do something
    endwhile;
endif; 
?>

But i strongly recommend you not to alter main query using query_posts() and instead use WP_Query as following.

<?php 
$query = new WP_Query( array( 'posts_per_page' => 2, 'meta_key' => '_bbp_sticky_topics', 'post_type'=> 'topic', 'order' => 'ASC' ) );
if ( $query->have_posts()) : 
    while ($query->have_posts()) : $query->the_post(); 
        //do something
    endwhile;
endif; 
/* Restore original Post Data */
wp_reset_postdata();
?>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.