WordPress.org

Support

Support » Plugins and Hacks » Co-Authors Plus » [Resolved] [Plugin: Co-Authors Plus] WP_Query() not working as it should

[Resolved] [Plugin: Co-Authors Plus] WP_Query() not working as it should

  • 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/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Daniel Bachhuber

    @danielbachhuber

    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.

    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!

    Plugin Author Daniel Bachhuber

    @danielbachhuber

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

    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!

    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.

    Plugin Author Daniel Bachhuber

    @danielbachhuber

    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 );
    ?>

    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.

    Plugin Author Daniel Bachhuber

    @danielbachhuber

    Odd. Glad you figured it out!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Resolved] [Plugin: Co-Authors Plus] WP_Query() not working as it should’ is closed to new replies.
Skip to toolbar