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


All you need is to use pre_get_posts action to hook to add author_id to the query based on your custom custom_user_id query var:

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: I see that you are flushing rewrite rules on every page load using init action hook. Flusing rewrite rules should be done only on plugin activation and deactivation. Other than that hooks, flushing rewrite rules should be done only specifically when you know that rewrite rules have changed and they need to be rebuilt. Note that flushing rewrite rules perform dabase operations that are not needed to do only one time, not on every page load.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.