如何根据Author_id显示存档页面帖子

时间:2015-09-13 作者:Shahrukh Khan

我想在自定义帖子的存档页面上显示帖子-gallery 基于author_id i、 e.theauthor_id 作为permalink变量传递。存档页面上显示的帖子将仅是作者发布的帖子author_id 已通过。

我的自定义帖子gallery 存档页面具有urlhttp://localhost/?post_type=gallery 在post\\u类型中发布的所有帖子-gallery 已列出。

function my_custom_gallery() {
        $gallery_labels = array(
            \'name\' => _x(\'Gallery\', \'post type general name\'),
            \'singular_name\' => _x(\'Gallery\', \'post type singular name\'),
            \'add_new\' => _x(\'Add New\', \'gallery\'),
            \'add_new_item\' => __("Add New Gallery"),
            \'edit_item\' => __("Edit Gallery"),
            \'new_item\' => __("New Gallery"),
            \'view_item\' => __("View Gallery"),
            \'search_items\' => __("Search Gallery"),
            \'not_found\' =>  __(\'No galleries found\'),
            \'not_found_in_trash\' => __(\'No galleries found in Trash\'), 
            \'parent_item_colon\' => \'\'               
        );
        $gallery_args = array(
            \'labels\' => $gallery_labels,
            \'public\' => true,
            \'publicly_queryable\' => true,
            \'show_ui\' => true, 
            \'query_var\' => true,
            \'rewrite\' => false,
            \'hierarchical\' => false,
            \'has_archive\'        => true,
            \'menu_position\' => null,
            \'capability_type\' => \'post\',
            \'supports\' => array(\'title\'),
            \'show_ui\' => true,
            \'query_var\' => true,
            \'menu_position\'      => 4,
            \'menu_icon\' =>  \'dashicons-images-alt2\'
        );

        register_post_type(\'gallery\', $gallery_args);

    }
    add_action( \'init\', \'my_custom_gallery\' );
我使用以下函数引入了一个自定义查询变量

function add_custom_query_var( $vars ){
  $vars[] = "custom_user_id";
  return $vars;
}
add_filter( \'query_vars\', \'add_custom_query_var\' );
我已经重写了URL结构,以包含custom_user_id

function add_rewrite_rules($aRules) {
$aNewRules = array(\'/gallery/user/([^/]+)?$\' => \'index.php?post_type=gallery&custom_user_id=$matches[1]\');
$aRules = $aNewRules + $aRules;
return $aRules;
}
add_filter(\'rewrite_rules_array\', \'add_rewrite_rules\'); 

function rewrite_flush(){
  global $wp_rewrite;
  $gallery_structure = \'/gallery/%user_id%/%gallery%\';
    $wp_rewrite->add_rewrite_tag("%gallery%", \'([^/]+)\', "gallery=");
    $wp_rewrite->add_permastruct(\'gallery\', $gallery_structure, false);
  $wp_rewrite->flush_rules();
}
add_action(\'init\',\'rewrite_flush\');


function tdd_permalinks($permalink, $post, $leavename){ 
    $no_data = get_the_author_meta(\'ID\');
    if($post->post_type != \'gallery\' || empty($permalink) || in_array($post->post_status, array(\'draft\', \'pending\', \'auto-draft\'))) return $permalink;
    $var1 = sanitize_title($no_data);
    $permalink = str_replace(\'%custom_user_id%\', $var1, $permalink); 
    return $permalink; 
}
add_filter(\'post_type_link\', \'tdd_permalinks\', 10, 3); 
我使用检索自定义查询变量$query_author_id = get_query_var(custom_user_id) 并希望显示作者具有author_id - $query_author_id 在存档页上。

1 个回复
最合适的回答,由SO网友:cybmeta 整理而成

您只需使用pre_get_posts 要挂接以添加的操作author_id 根据您的自定义custom_user_id 查询变量:

add_action( \'pre_get_posts\', \'filter_by_custom_user_id\' );
function filter_by_custom_user_id( $query ) {

    // Apply only on frontend, for gallery post type archive, for main query and if custom_user_id query var is set.
    if( ! is_admin()
        && $query->is_main_query()
        && is_post_type_archive( \'gallery\' )
        && isset( $query->query_vars[\'custom_user_id\'] ) {

            $query->set( \'author_id\', (int) $query->query_vars[\'custom_user_id\'] );
    }

}
IMPORTANT: 我看到您正在使用刷新每个页面加载上的重写规则init 行动挂钩。Flusing重写规则只能在插件激活和停用时执行。除此之外,只有当您知道重写规则已经更改并且需要重新构建时,才应该专门刷新重写规则。请注意,刷新重写规则执行的dabase操作不需要只执行一次,也不需要每次加载页面。

相关推荐

Let me choose permalinks

我需要选择一个叫做“mysite”的永久链接。com/1418”,但wordpress不断在永久链接中添加“-2”。通常这意味着我已经有了一个名为“相同”的页面,它位于垃圾箱或其他地方。但这里的情况似乎并非如此。我尝试在设置中重置永久链接,这也没有帮助。我如何使用数字作为页面名称permalink,而不用wordpress在permalink中添加“-2”。