AnsPress_Profile_Hooks::modify_query_archive( array $posts, object $query )

Description #

Modify main query.

Parameters #

  • $posts
    array (Required) Array of post object.
  • $query
    object (Required) Wp_Query object.

Changelog #

VersionDescription
4.1.2Check for 404 error.
4.1.1Redirect to current user profile if no author set.
4.1.0Introduced.

Source #

File: addons/free/profile.php

	public static function modify_query_archive( $posts, $query ) {
		if ( $query->is_main_query() && ! $query->is_404 && 'user' === get_query_var( 'ap_page' ) ) {
			$query_object = get_queried_object();

			if ( ! $query_object && ! get_query_var( 'author_name' ) && is_user_logged_in() ) {
				wp_safe_redirect( ap_user_link( get_current_user_id() ) ); exit;
			} elseif ( $query_object && $query_object instanceof WP_User ) {
				return [ get_post( ap_opt( 'user_page' ) ) ];
			} else {
				$query->set_404();
				status_header( 404 );
			}
		}
        
        return $posts;
        
	}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment