$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to get bbpress sticky topics|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to get bbpress sticky topics

matteradmin9PV0评论

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?

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?

Share Improve this question edited Aug 23, 2013 at 5:48 Arslaan Ejaz asked Jul 30, 2013 at 21:18 Arslaan EjazArslaan Ejaz 6371 gold badge8 silver badges20 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 2 +50

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();
?>

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.

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() );

Another answer, from this bbpress thread

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far