How to display archive page posts based on author_id
I would like the posts to be displayed on archive page of custom post - gallery
based on author_id
i.e. the author_id
is passed as permalink variable. The posts displayed on archive page would be only those posts published by author whose author_id
is passed.
My custom post gallery
archive page has the url http://localhost/?post_type=gallery
where all the posts posted in the post_type - gallery
are listed.
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' );
I have introduced a custom query variable using following function
function add_custom_query_var( $vars ){
$vars[] = "custom_user_id";
return $vars;
}
add_filter( 'query_vars', 'add_custom_query_var' );
I have rewritten my URL structure to contain the custom_user_id
function add_rewrite_rules($aRules) {
$aNewRules = array('/gallery/user/([^/]+)?$' = 'index.php?post_type=gallerycustom_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);
I retrive the custom query variable using $query_author_id = get_query_var(custom_user_id)
and want the posts displayed whose author has the author_id
- $query_author_id
on the archive page.
Topic authorization custom-post-type-archives permalinks Wordpress
Category Web