WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Author template Multiple Custom Post type queries (15 posts)

  1. 10PL8
    Member
    Posted 1 year ago #

    Hi all, I am an extreme WP novice... and am basically trying to figure out as I go.

    I have created a Author page for a WP site, where I want to display each of the authors published CPT's separately/categorically. I have managed to achieve this by doing separate WP_Query's for each CPT and then resetting the query each time etc...

    Here is my code:

    <div class="tab-container" id="tab-container">
    
    <ul class="etabs">
    <li class="tab active"><a href="#maintab1" class="active">TAB1</a></li>
    <li class="tab"><a href="#maintab2" >TAB2</a></li>
    <li class="tab"><a href="#maintab3" >TAB3</a></li>
    </ul>                                    
    
    <div class="panel-container">
    
    <div id="maintab1">
    <div id="tab-container-inner">
    
    <?php
    
    echo '<ul style="margin-left: 75px;" class="etabs">';
    
    $postTypes =  array('post_type1', 'post_type2', 'post_type3', 'post_type4');
    $loopTypes = new WP_Query( array( 'post_type' => $postTypes, 'author' => $curauth->ID ));
    
    foreach ($postTypes as $postType) {
    if ( $loopTypes->have_posts() ) {
    echo '<li class="tab"><a href="#'.$postType.'"><span class="tab">'.$postType.'</span></a></li>';
    }else{
    return;
    }
    
    }
    
    wp_reset_query();
    echo '</ul>';
    
    ?>
    <div>
    <?php 
    
    $current_cpt = 'post_type1';
    $loop = new WP_Query( array( 'post_type' => $current_cpt, 'author' => $curauth->ID, 'posts_per_page' =>-1, 'orderby' => 'title', 'order' => 'DESC'));
    if ( $loop->have_posts() ) :
    $obj = $wp_post_types[$current_cpt];
    echo '<div id="'.$current_cpt.'">';
    echo '<h2>'.$obj->labels->singular_name.'</h2>';
    ?>
    <div id="portfolio-image-wrap"><span id="portfolio-image-wrap-shadow"></span>
    <img id="main-image" class="<?php echo $current_cpt;?>">
    <?php 
    
    echo '<div>';
    echo '<ul id="items" class="' . $current_cpt . '">';
    
    while ($loop->have_posts()) : $loop->the_post();
    echo '<div></div>'; //Display some layout stuff $current_cpt 
    
    endwhile;
    echo "</ul>";
    wp_reset_query();
    wp_reset_postdata();
    $current_cpt = '';
    
    echo '</div>';
    endif;
    
    $current_cpt = 'post_type2';
    $loop = new WP_Query( array( 'post_type' => $current_cpt, 'author' => $curauth->ID, 'posts_per_page' =>-1, 'orderby' => 'title', 'order' => 'DESC'));
    if ( $loop->have_posts() ) :
    $obj = $wp_post_types[$current_cpt];
    echo '<div id="'.$current_cpt.'">';
    echo '<h2>'.$obj->labels->singular_name.'</h2>';
    ?>
    <div id="portfolio-image-wrap"><span id="portfolio-image-wrap-shadow"></span>
    <img id="main-image" class="<?php echo $current_cpt;?>">
    <?php 
    
    echo '<div>';
    echo '<ul id="items" class="' . $current_cpt . '">';
    
    while ($loop->have_posts()) : $loop->the_post();
    echo '<div></div>'; //Display some layout stuff $current_cpt 
    
    endwhile;
    echo "</ul>";
    wp_reset_query();
    wp_reset_postdata();
    $current_cpt = '';
    
    echo '</div>';
    endif;
    
    $current_cpt = 'post_type3';
    $loop = new WP_Query( array( 'post_type' => $current_cpt, 'author' => $curauth->ID, 'posts_per_page' =>-1, 'orderby' => 'title', 'order' => 'DESC'));
    if ( $loop->have_posts() ) :
    $obj = $wp_post_types[$current_cpt];
    echo '<div id="'.$current_cpt.'">';
    echo '<h2>'.$obj->labels->singular_name.'</h2>';
    ?>
    <div id="portfolio-image-wrap"><span id="portfolio-image-wrap-shadow"></span>
    <img id="main-image" class="<?php echo $current_cpt;?>">
    <?php 
    
    echo '<div>';
    echo '<ul id="items" class="' . $current_cpt . '">';
    
    while ($loop->have_posts()) : $loop->the_post();
    echo '<div></div>'; //Display some layout stuff $current_cpt 
    
    endwhile;
    echo "</ul>";
    wp_reset_query();
    wp_reset_postdata();
    $current_cpt = '';
    
    echo '</div>';
    endif;
    ?>
    </div>
    </div>
    </div>
    
    <div id="maintab2">
    </div>
    <div id="maintab3">
    </div>

    I need help with two things:
    - I know this is not good practice to achieve this???
    - How do i get authors post types, where he/she has posted in ONLY???

  2. 10PL8
    Member
    Posted 1 year ago #

    Any help would be greatly appreciated... I have literally spent days trying to work this out...

  3. 10PL8
    Member
    Posted 1 year ago #

    Anyone??? I searched the support forums here and all over the net and was unable to find any sort of answer's to my questions...

    Any help, even just to point me in the right direction.

    please please help...

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    To get the post types that authors have posted in you can use this your theme's functions.php:

    function get_author_post_types( $author_id = false ) {
    	$results = array();
    	if ( $author_id ) {
    		$post_types= get_post_types( array( 'public' => true ), 'names', 'and' );
    		$post_types = array_keys( $post_types );
    		$sql = array();
    		global $wpdb;
    		$sql[] = "SELECT DISTINCT post_type FROM $wpdb->posts WHERE post_author = %d AND (post_status = 'publish') AND post_type IN(".implode( ', ', array_fill( 0, count( $post_types ), '%s' ) ).")";
    		$sql[] = $author_id;
    		$sql = array_merge( $sql, $post_types );
    		$query = call_user_func_array( array( $wpdb, 'prepare' ), $sql );
    		$results = $wpdb->get_results( $query );
    		if ( $results )
    			$results = wp_list_pluck( $results, 'post_type' );
    	}
    	return $results;
    }

    In your author template you are now able to get an array of post types by using something similar to this:

    <?php
    // get post types for author with id 4
    $author_post_types = get_author_post_types( 4 );
    if ( !empty( $author_post_types ) ) {
    	foreach ( $author_post_types as $post_type ) {
    		echo 'this author posted in ' . $post_type;
    	}
    }
    ?>
  5. 10PL8
    Member
    Posted 1 year ago #

    keesiemeijer that worked perfectly - your a legend!!!

    Last question and I promise to leave you alone :)

    How would I go about doing either (Which ever is easiest):
    - Exclude a specific cpt from query
    OR
    - Retrieve results from an array of post types

    Thanks a million!!!

  6. keesiemeijer
    moderator
    Posted 1 year ago #

    Retrieve results from an array of post types is easier.
    Just use it as you already do:

    $author_post_types = get_author_post_types( 4 );
    $loopTypes = new WP_Query( array( 'post_type' => $author_post_types, 'author' => $curauth->ID ));

    Exluding a post type would be similar to first retrieving all post types and then excluding it from the array:

    // get all post types (post, page, attachment, custom post types )
    $post_types = get_post_types( array( 'public' => true ), 'names', 'and' );
    
    // remove post type "post_type1" from $post_types array
    unset($post_types['post_type1']);
    
    // use array keys value from $post_types array
    $post_types = array_keys( $post_types )
    
    // query post types
    $loopTypes = new WP_Query( array( 'post_type' => $post_types, 'author' => $curauth->ID ));

    http://codex.wordpress.org/Function_Reference/get_post_types

  7. 10PL8
    Member
    Posted 1 year ago #

    That worked beautifully, thank you very much for your help Keesiemeijer.

  8. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome. I'm glad you've got it resolved :-)

  9. 10PL8
    Member
    Posted 1 year ago #

    Keesiemeijer - last question (I promise this time :))

    I loved your get_author_post_types() function, works exactly how i want it to, to exclude a specific CPT I included the "unset($post_types['post_type1']);" in the actual function to exclude a specific CPT...

    I have tried incorporating the get_author_post_types() function to display my CPT's to limit the WP_query's... But every time it returns all posts in all CPT's, is there any way other than doing individual query's and reseting it each time for each CPT to separate/categorize each CPT???

    I am super grateful for all your help :)

  10. keesiemeijer
    moderator
    Posted 1 year ago #

    If you want to show the custom post types seperated on the page you have to query for them seperately.

    How did you try to incorporate the function to display the CPT's?

  11. 10PL8
    Member
    Posted 1 year ago #

    Nothing special, i essentially used the array of CPT's created by your function in my WP_query...

  12. keesiemeijer
    moderator
    Posted 1 year ago #

    The function returns all post types the author has posted in. To show the post types posts separately you can loop through them. Example [untested]:

    <?php
    // get post types for author with id 4
    $author_post_types = get_author_post_types( 4 );
    if ( !empty( $author_post_types ) ) :
    	foreach ( $author_post_types as $post_type ) :
    ?>
    <?php
    $loop = new WP_Query( array( 'post_type' => $post_type, 'author' => $curauth->ID ));
    ?>
    
    <!-- the loop (used for all author post types) -->
    <?php while ($loop->have_posts()) : $loop->the_post(); ?>
    <!-- put the rest of the loop here -->
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>
    
    <?php  endforeach; ?>
    <?php endif; ?>
  13. 10PL8
    Member
    Posted 1 year ago #

    That's exactly how I tried to implement it. It does exactly what its supposed to do.

    Essentially what I am trying to output for each post type is:

    <div id="uniqueID">
    <h2>Post type name</h2>
    <div><img></div><!--This gets manipulated using jQuery src attr-->
    <ul>
    <!--
    list all relavant posts to current CPT.
    these li's are thumbnails which change the above image when clicked
    -->
    <li>Post type, post 1</li>
    <li>Post type, post 2</li>
    </ul>
    </div>

    The only way I could think of achieving this is getting the counts per post type / per author then outputting the closing tags based on that.

    I am using your function to output a set of links to each of these and then display the relevant <div id=uniqueID"> depending on which link is clicked using jQuery.

    It works well duplicating the WP_Query and loop. But I'm worried about server stress. I currently have 7 query's (Including the query in your function) - is that too many?

  14. 10PL8
    Member
    Posted 11 months ago #

    keesiemeijer, once again - thank you for your help :)

    I have another question if possible...

    Is there anyway to tailor that function to fetch a specific Post type, but only return the taxonomies that have been posted in.

    I'm basically trying to do the same as the first, but this time display the CPT (there's only one) categorized by Taxonomy, or do i have to do individual queries for each taxonomy (there are 18) ?

    Thanks in advance.

  15. 10PL8
    Member
    Posted 11 months ago #

    This is what I have so far, I know it needs a lot more work though (Getting loads of errors)

    function get_author_post_taxs( $author_id = false ) {
    	$results = array();
    	if ( $author_id ) {
    
    		$tax_types = get_terms( 'photograph-subject', 'orderby=count&hide_empty=0' );
    
    		$tax_types = array_keys( $tax_types );
    		global $wpdb;
    		$sql = "SELECT DISTINCT post_type FROM $wpdb->posts WHERE post_author = %d AND (post_status = 'publish') AND post_type IN(".implode( ', ', array_fill( 0, count( $post_types ), '%s' ) ).")";
    		$sql = array_merge( array( $sql ), array( $author_id ) );
    		$sql = array_merge( $sql, $post_types );
    		$query = call_user_func_array( array( $wpdb, 'prepare' ), $sql );
    		$results = $wpdb->get_results( $query );
    		if ( $results )
    			$results = wp_list_pluck( $results, 'taxonomy' );
    	}
    	return $results;
    }

    Any help would be very much appreciated :) or any suggestions or direction...

    Please i'm desperate.

Topic Closed

This topic has been closed to new replies.

About this Topic