WordPress.org

Ready to get started?Download WordPress

Forums

Co-Authors Plus
[resolved] WP_Query() not working as it should (9 posts)

  1. mkubilayk
    Member
    Posted 2 years ago #

    I am using Co-Authors Plus 2.6.1 and I am trying to show the latest post of each author in the sidebar. Here is the code I am using: ($aUsersID is the sorted user-id list)

    foreach ( $aUsersID as $iUserID ) {
    	$auth = 'author=' . $iUserID;
    	...
    	$author_posts = new WP_Query($auth);
    	...
    	...
    }

    This works great for single-author posts however it does not show the post of a co-author unless s/he is the original author. I can get around this by searching all posts for each author and check whether s/he is in the coauthors list or not and break the while if the case is satisfied. This looks like a definite solution but it is not quite efficient. How can I find a way out besides this one?

    Thanks in advance...

    http://wordpress.org/extend/plugins/co-authors-plus/

  2. Daniel Bachhuber
    Hand Built
    Plugin Author

    Posted 2 years ago #

    You're correct in that doing a normal WP_Query will only produce the posts where the author is set as the primary author. When your co-authors data is saved, the first author on the list is saved as the primary author, and all authors are saved as terms for the post under the 'author' taxonomy.

    So, the simplest thing to do is to search for all posts with the author set as a taxonomy term. This works much like a normal 'tag' or 'category' query would.

    Let me know if you have any questions as to what the code might look like.

  3. dpfuhl
    Member
    Posted 1 year ago #

    I've found this post via google and identified my problem with the plugin as the same. I was not successful in writing the correct code for WP_Query, so I would really be glad to get an example here, as you offered.

    Many thanks in advance!

  4. Daniel Bachhuber
    Hand Built
    Plugin Author

    Posted 1 year ago #

    @dpfuhl could you describe a bit more about what you're trying to accomplish?

  5. dpfuhl
    Member
    Posted 1 year ago #

    You offered to give a code example for WP_Query for the authors taxonomy term in your post above:

    So, the simplest thing to do is to search for all posts with the author set as a taxonomy term. This works much like a normal 'tag' or 'category' query would.

    Let me know if you have any questions as to what the code might look like.

    This is what I would like to get, thanks!

  6. dpfuhl
    Member
    Posted 1 year ago #

    Right now I'm using this query to get all posts of category ID 4 and the current author:

    <?php
             $author_ID = $post->post_author;
             $args = array(
             'cat' => '4',
             'author' => $author_ID,
             'post_type' => 'post',
             'orderby' => 'meta_value',
             'meta_key' => 'morefields_date',
             'order' => 'ASC'
            );
            $events_query = new WP_Query( $args );
            ?>

    The result is showing only posts were the current author is the primary one. But I would like to get all posts of the current author, of course.

  7. Daniel Bachhuber
    Hand Built
    Plugin Author

    Posted 1 year ago #

    Sure, you can use something like this:

    <?php
    	$user_login = 'your-user-login';
    	$args = array(
    		'cat' => '4',
    		'post_type' => 'post',
    		'orderby' => 'meta_value',
    		'meta_key' => 'morefields_date',
    		'order' => 'ASC'
    		'tax_query' => array(
    			array(
    				'taxonomy' => 'author',
    				'field' => 'slug',
    				'terms' => $user_login,
    			),
    		),
    	);
    	$events_query = new WP_Query( $args );
    ?>
  8. dpfuhl
    Member
    Posted 1 year ago #

    Success! Thank you very much!

    Only some fixes have been neccessary:

    <?php
            $author_ID = $post->post_author;
            $user_info = get_userdata($author_ID);
          	$user_login = $user_info->user_login;
    	      $args = array(
    		      'tax_query' => array(
    			       array(
    				      'taxonomy' => 'author',
    				      'field' => 'slug',
    				      'terms' => $user_login
    			       )
    		      ),
    		      'cat' => '4',
    		      'post_type' => 'post',
    		      'orderby' => 'meta_value',
    		      'meta_key' => 'morefields_date',
    		      'order' => 'ASC'
    	      );
    ?>

    For some reason the tax_query array has to be the first element in the args array. If not, there will be a parsing error.

  9. Daniel Bachhuber
    Hand Built
    Plugin Author

    Posted 1 year ago #

    Odd. Glad you figured it out!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic