What is the easiest way to create a custom field archive?

To create a custom field archive in WordPress where posts are filtered by a user_submit_name custom field, follow these steps:

  1. Register a Query Variable: This enables WordPress to recognize and use this variable in the URL.

    function register_query_vars( $vars ) {
        $vars[] = 'user_submit_name';
        return $vars;
    }
    add_filter( 'query_vars', 'register_query_vars' );
    
  2. Modify the Archive Query: Adjust the existing pre_get_posts function to use the newly registered query variable.

    function wpd_author_query( $query ) {
        if ( $query->is_author() && $query->is_main_query() ) {
            $user_submit_name = get_query_var('user_submit_name');
            if (!empty($user_submit_name)) {
                $query->set( 'meta_key', 'user_submit_name' );
                $query->set( 'meta_value', $user_submit_name );
                $query->set( 'posts_per_page', 4 );
                unset( $query->query_vars['author_name'] );
            }
        }
    }
    add_action( 'pre_get_posts', 'wpd_author_query' );
    

This setup enables filtering posts by the custom field user_submit_name without requiring hardcoded values.