如何通过在Wordpress中为此函数添加过滤器来覆盖BBPress函数bbp_has_topics()?

时间:2015-01-21 07:46:48

标签: wordpress

" bbp_has_topics "功能在" public / wp-content / plugins / bbpress / includes / topics / template.php "通过" meta_value "提取主题在" 降序"订购。我需要覆盖此行为以通过" 创建日期"来获取帖子不编辑此文件。那么有没有办法为这个" bbp_has_topics"添加一个过滤器。我在 functions.php 中添加了一个过滤器,如下所示。但是在列表页面中没有显示分页链接。

function mybbp_has_topics($ args =''){     $ bbp = bbpress();

// Other defaults
$default_topic_search  = !empty( $_REQUEST['ts'] ) ? $_REQUEST['ts'] : false;
$default_show_stickies = (bool) ( bbp_is_single_forum() || bbp_is_topic_archive() ) && ( false === $default_topic_search );
$default_post_parent   = bbp_is_single_forum() ? bbp_get_forum_id() : 'any';

// The default forum query for most circumstances
$default = array(
    'post_type'      => bbp_get_topic_post_type(), // Narrow query down to bbPress topics
    'post_parent'    => $default_post_parent,      // Forum ID
    'meta_key'       => '_bbp_last_active_time',   // Make sure topic has some last activity time
    'orderby'        => 'date',              // 'meta_value', 'author', 'date', 'title', 'modified', 'parent', rand',
    'order'          => 'DESC',                    // 'ASC', 'DESC'
    'posts_per_page' => bbp_get_topics_per_page(), // Topics per page
    'paged'          => bbp_get_paged(),           // Page Number
    's'              => $default_topic_search,     // Topic Search
    'show_stickies'  => $default_show_stickies,    // Ignore sticky topics?
    'max_num_pages'  => false,                     // Maximum number of pages to show
);

$r = bbp_parse_args( $args, $default, 'has_topics' );

// Run the query
$bbp->topic_query = new WP_Query( $r );

return apply_filters( 'mybbp_has_topics', $bbp->topic_query->have_posts(), $bbp->topic_query );

}

1 个答案:

答案 0 :(得分:0)

根据我对此的理解,您需要将过滤器添加到bbp_has_topics中的钩子中。

应该在新插件或主题的functions.php中添加类似此功能的内容。

function mybbp_has_topics($have_posts, $topic_query){
    //your code
    $default_topic_search  = !empty( $_REQUEST['ts'] ) ? $_REQUEST['ts'] : false;
    $default_show_stickies = (bool) ( bbp_is_single_forum() || bbp_is_topic_archive() ) && ( false === $default_topic_search );
    $default_post_parent   = bbp_is_single_forum() ? bbp_get_forum_id() : 'any';

    // The default forum query for most circumstances
    $default = array(
        'post_type'      => bbp_get_topic_post_type(), // Narrow query down to bbPress topics
        'post_parent'    => $default_post_parent,      // Forum ID
        'meta_key'       => '_bbp_last_active_time',   // Make sure topic has some last activity time
        'orderby'        => 'date',              // 'meta_value', 'author', 'date', 'title', 'modified', 'parent', rand',
        'order'          => 'DESC',                    // 'ASC', 'DESC'
        'posts_per_page' => bbp_get_topics_per_page(), // Topics per page
        'paged'          => bbp_get_paged(),           // Page Number
        's'              => $default_topic_search,     // Topic Search
        'show_stickies'  => $default_show_stickies,    // Ignore sticky topics?
        'max_num_pages'  => false,                     // Maximum number of pages to show
);

    $r = bbp_parse_args( $args, $default, 'has_topics' );

    // Run the query
    $topic_query = new WP_Query( $r );
    return $topic_query;}

add_filter( 'bbp_has_topics', mybbp_has_topics, 99, 2);

参数$ topic_query包含在bbp_has_topics中进行的查询。