0

So, here is the issue:

I am using a plugin named User Submitted Posts, and it works great on its part. However, I still want to have that user content page the Twenty Twenty Four theme has by default, which you can access when you click on the user's name. The issue is, USP doesn't require logins, so these posts are all actually submitted by the same user, whose display name is being overridden to show as the user_submit_name, a custom field in USP's posts.

This makes the author archive display all the posts, because technically they are all assigned the same author.

What I want is to modify the query on the archive page to sort through posts using this custom field, so that it still works. I am a complete beginner though, so this is proving rather difficult.

The main issue I'm facing is that I know how to modify the author archive query, but I don't know how to pass the user_submit_name it should use to the function. Basically, I have no way of telling the archive for which name it should be sorting, aside from hard coding it.

Here is my code so far:

function wpd_author_query( $query ) {
if ( $query->is_author()
    && $query->is_main_query() ) {
        // your code to set $current_user_name here
        $query->set( 'meta_key', 'user_submit_name' . /*author name*/);
        $query->set( 'posts_per_page', 4);

        // EDIT
        // unset the requested author
        unset( $query->query_vars['author_name'] );
}
}
add_action( 'pre_get_posts', 'wpd_author_query' );

To recap, I need to somehow pass the custom field "user_submit_name" from a post to the function that modifies the author query, when the viewer clicks on the author link.

Does anyone know how I could go about this? Also if you have any better ideas how to do this, which you probably do, feel free to share them. Thank you.

1 Answer 1

0

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.

1
  • Thank you, but do you have an idea how I could pass the $user_submit_name value to the function when the user clicks the author link? Because currently it has no access to that data from the archive page itself, which leaves the variable as empty.
    – Grond
    Commented Jun 10 at 7:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.