• Resolved sikorat

    (@sikorat)


    Hi,
    Im coding a wordpress template and i have this plugin installed.
    I’m trying to query all posts of single author, by all i mean all posts where he/she is only author and he/she is couathor of some post.
    Im using this simple query to achieve this. How to modify it to get all posts including coauthored posts?

    <ul data-author="<?php echo $personID?>" class="resources__list">
    	?php
    	$args = array(
    	'post_status' => 'publish',
    	'posts_per_page' => 3,
    	'post_type' => array('video'),
    	'author__in' => $person -> ID
    	);
    	$knowledge_questions = new WP_Query( $args );
    	if ( $knowledge_questions->have_posts() ) : ?>
    		<?php while ( $knowledge_questions->have_posts() ) : $knowledge_questions->the_post(); ?>
    	<?php get_template_part( 'template-parts/content', 'article' );?>
    	<?php 
    		endwhile; 
    		wp_reset_postdata();?>
    	<?php endif; ?>
    </ul>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello @sikorat ??

    The following query fetches all posts of a certain author and/or guest author:

    $args = array(
      'post_type' 	   => 'post',
      'author_name'	   => $person->user_nicename,
      'posts_per_page' => -1
    );
    
    $query = new WP_Query( $args );
    $query->posts;

    I also noticed two mistakes in your code:

    1. $person -> ID → This variable should not contain spaces. The correct version should be $person->ID.
    2. author__in → This parameter expects an array. The correct call would be 'author__in' => array( $person->ID ).

    As for the snippet I posted above, please note that author_name works with both authors and guest authors, while author, author__in and author__not_in only works with authors, but not with guest authors.

    Thread Starter sikorat

    (@sikorat)

    Hi thank you for this reply, but sadly this doesn’t work.

    $args              = array(
    'post_status'    => 'publish',
    'posts_per_page' => 3,
    'author_name'	   => $person->user_nicename,
    'post_type'      => 'video',
    );

    With this args it doesn’t show video’s(custom post type) by current user, it shows posts of this user but not from this custom post type.
    When I delete “author_name” query is showing posts of this custom post type, so it looks like it is issue white this field and not with post_type.
    When i use 'author__in' => $person -> ID I see posts of this user in this cpt but not guest posts.
    Do you know how to fix it?

    @sikorat
    Co Authors Plus taxonomy ‘author’ is attached to the post_type ‘guest-author’. The tax_query looks up ‘term_relationship’ table for posts by assigned coauthor_id (post_id) from the ‘term_taxonomy_id’ matched by taxonomy = ‘author’ and term_id = ‘$person->ID’. I’m not sure the $person->ID you obtain is a guest-author or a user.

    
    /**
     * WP_Query args setting
     * @link https://developer.www.ads-software.com/reference/classes/wp_query/
     */
    $args = array(
    	/* to search all post types, set to 'any' 
    	 * or omit, defaults to 'any' when tax_query is set
    	 * or limit multiple values with array( 'video', 'post' )
    	 */
    	'post_type'      => 'video', 
    	'post_status'    => 'publish',  //omit, default is 'publish'
    	'posts_per_page' => 3,
    	'tax_query' => array( array( 
    		'taxonomy' => 'author',
    		'field'    => 'term_id',
    		'terms'    => $person->ID,
    	) ) 
    );

    Example in use:

    
    /**
     * WP PHP coding standards
     * @link https://make.www.ads-software.com/core/handbook/best-practices/coding-standards/php/
     */
    
    /**
     * Check if on an existing author archive page
     * Guest author is not a user, therefore the typical 'author_id' is not a user_id.
     * Guest author is stored as a post, guest_id = post_id
     *
     * @link https://developer.www.ads-software.com/reference/functions/is_author/
     * @link https://developer.www.ads-software.com/reference/functions/get_queried_object/
     */
    if ( is_author() ) : 
    	$guests = array( get_queried_object() );
    elseif ( is_single() ) :
    	$guests = get_coauthors();
    else :
    	return; //no point continuing
    endif;
    
    foreach ( $guests as $guest ) : 
    	$args = array(
    		'post_type'      => 'video', //or array( 'video', 'post' )
    		'posts_per_page' => 3,
    		'tax_query' => array( array( 
    				'taxonomy' => 'author',
    				'field'    => 'term_id',
    				'terms'    => $guest->ID,
    		)	) 
    	);
    
    	$kq = new WP_Query( $args );
    
    	if ( $kq->have_posts() ) :
    		?>
    
    		<ul data-author="<?php echo $guest->ID; ?>" class="resources__list">
    			<?php
    			while ( $kq->have_posts() ) : $kq->the_post(); 
    
    				get_template_part( 'template-parts/content', 'article' );
    
    			endwhile; 
    			wp_reset_postdata();
    			?>
    		</ul>
    
    		<?php
    	else :
    		echo "<!-- 'no posts matched query' -->";
    	endif;
    endforeach;

    @backbone

    Thanks for this code – it is essentially what I needed for a project I am working on however, I am trying to pull posts from another site in a WP Multisite network. I am essentially using the same code but it is returning no results. Any thoughts here?

    Essentially here is what I am doing:

    
    $author_ID = get_sub_field('author_id');
    switch_to_blog(2);
    $posts = get_posts(
                      array(
                        'post_type' => 'post',
                        'posts_per_page' => 4,
                        'tax_query' => array( array( 
    		      'taxonomy' => 'author',
    		      'field'    => 'term_id',
    		      'terms'    => $author_ID,
    	             ) )
                      )
                    );
    

    Based on some research, it seems with multisite the taxonomy needs to exist on the site you are querying from, in this case blog 1. So, I did enable Co-Authors plugin on that site and created a test post with multiple authors but still no results.

    Thoughts?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Query all posts of author even if he/she is co-author’ is closed to new replies.