Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

4 Answers

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.

share|improve this answer
    
thanks Alberto i will check this. –  arslaan ejaz Nov 23 '13 at 7:20
add comment

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();
?>
share|improve this answer
    
thanks, let me try this. –  arslaan ejaz Aug 20 '13 at 5:48
    
@arslaanejaz Let me know if you have any issues. –  Vinod Dalvi Aug 20 '13 at 6:08
    
thanks for you answer but my friend no luck. i m not getting any record. –  arslaan ejaz Aug 20 '13 at 8:30
    
but i am heaving this value in wp_postmeta. _bbp_sticky_topics => a:1:{i:0;i:632;} –  arslaan ejaz Aug 20 '13 at 8:51
    
Can you shoe me your updated code? –  Vinod Dalvi Aug 20 '13 at 10:10
show 4 more comments

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.

share|improve this answer
    
That's nice... but do you want to display forum title or topic title? –  Vinod Dalvi Aug 20 '13 at 13:08
    
@Vinod i m displaying forum title for now but i think the same approach is not possible with topic. forums is working so we can extend the functionality to get title. –  arslaan ejaz Aug 20 '13 at 15:06
    
Topic is also working at my side. IF you provide me more information about this from your side then we can resolve it. –  Vinod Dalvi Aug 20 '13 at 16:57
    
@Vinod i tried tens of time and i m not getting any sticky post record by topic. i don't know where the problem was. are you using hierarchical forums? i m using... i really want to approve your answer but for that it should be recommended answer. –  arslaan ejaz Aug 22 '13 at 6:12
    
I am not using any hierarchical forums. I have just installed bbpress plugins created some topics and made some of them sticky. Are you following the same steps or different? –  Vinod Dalvi Aug 23 '13 at 5:32
show 3 more comments

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() );
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.