Atticweb Atticweb - 5 months ago 17
PHP Question

Wordpress: Bbpress add extra filter to the popular view

I am currently working on a Wordpress website with a Bbpress forum installed. I am using a shortcode to add all the popular topics on a (non-forum)page:

[bbp-single-view id="popular"]


I want to add a extra filter to this function and I can't figure out how to do this. I would like to filter on the meta value '_bbp_last_active_time', to check if there was any activity on a topic in the last month.

I found this script in the bbpress core, but I don't know how I can add another filter:

// bbpress.php line 672

bbp_register_view(
'popular',
__( 'Most popular topics', 'bbpress' ),
apply_filters( 'bbp_register_view_popular', array(
'meta_key' => '_bbp_reply_count',
'max_num_pages' => 1,
'orderby' => 'meta_value_num',
'show_stickies' => false
)
) );


I did try something like this with SQL and a custom shortcode, but I don't know how to convert this to a list of topics with the correct layout functions:

SELECT
wp_posts.post_parent, COUNT(*) as count, wp_postmeta.*
FROM
wp_posts, wp_postmeta
WHERE
wp_posts.post_type = 'reply'
AND
wp_postmeta.post_id = wp_posts.post_parent
AND
wp_postmeta.meta_key = '_bbp_last_active_time'
AND
wp_postmeta.meta_value > (NOW() - INTERVAL 1 MONTH)
GROUP BY
wp_posts.post_parent
ORDER BY
count DESC
LIMIT 5;


Thanks in advance! Help is much appreciated!

Answer

I already found a solution:

$date_now = date( 'Y-m-d H:i:s' );
$date=date_create($date_now);
date_sub($date,date_interval_create_from_date_string("30 days"));
$new_date = date_format($date,"Y-m-d H:i:s");
bbp_register_view(
    'jaappopular',
    __( 'Most popular topics', 'bbpress' ),
    apply_filters( 'bbp_register_view_popular', array(
        'meta_key'      => '_bbp_reply_count',
        'max_num_pages' => 1,
        'orderby'       => 'meta_value_num',
        'show_stickies' => false,
        'meta_query' => array(
            'relation'  => 'AND',
            array(
                'key'     => '_bbp_last_active_time' ,
                'value'   => $new_date,
                'compare' => '>=',
            )
        ),
    )
) );
);

YEAH!