Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Display author's CPT in all their normal posts WordPress

  • SOLVED

- Each author creates 1 CPT
- Each author creates many normal posts
- Each normal post of each author has their CPT added into normal post

I'm looking for a query snippet that will allow the above. Essentially, I need a query that i can paste into single.php and display any info from the author's CPT.

This snippet <strong>might</strong> help (it displays ALL CPT posts, not just the author's post)?

<?php
$args2=array('author'=>$authorid,'post_type'=>'school_profile', 'numberposts'=> -1);
$cquery2=new WP_Query($args2);
if($cquery2->have_posts()):
while($cquery2->have_posts()):
$cquery2->the_post();
?>

<p><?php the_title();?></p>

<?
endwhile;
wp_reset_postdata();
endif;
?>



my single.php

<?php
/**
* The Template for displaying all single posts.
*
* @package GeneratePress
*/

get_header(); ?>

<div id="primary" <?php generate_content_class();?>>
<main id="main" <?php generate_main_class(); ?>>
<?php do_action('generate_before_main_content'); ?>
<?php while ( have_posts() ) : the_post(); ?>

<?php get_template_part( 'content', 'single-post' ); ?>

<?php
// If comments are open or we have at least one comment, load up the comment template
if ( comments_open() || '0' != get_comments_number() ) : ?>
<div class="comments-area">
<?php comments_template(); ?>
</div>
<?php endif; ?>

<?php endwhile; // end of the loop. ?>
<?php do_action('generate_after_main_content'); ?>
</main><!-- #main -->
</div><!-- #primary -->

<?php
do_action('generate_sidebars');
get_footer();

Answers (4)

2016-04-15

Andrea P answers:

by CPT do you mean "custom post type"?
a custom post type is a container like "pages", or "events", and then inside the CPT you can create "custom posts", as you do when you create single pages or single posts (same would be for single events or any other single custom post).

are you sure you want to set up a custom post type for each author? this means that if you have 50 authors your wp will set up 50 custom post types in your db tables.. sounds a bit overloaded to me..

in particular because it is not necessary in my opinion... can't you create just one custom post_type and then use a custom taxonomy to divide its posts among different Authors?
(and then in the secondary loop you'd loop all the posts from the term(s) related to that user)

not to mention that having different authors is already a way to divide the posts, as you can easily query posts from a single author (as per your sample snippet), therefore you wouldn't probably need the taxonomy at all, and just display all the posts from the currently viewed author id..

also, in order to have one CPT for each author, you'll need a tricky code which would loop each user and set up a custom post type for each of them, and this will have to run every wp-head load.. I can't see another way to do it, if not by manually creating a cpt every time a new author is registered...


pjeaje comments:

No sorry a single post of a single CPT


Andrea P comments:

ah ok!
so it seems you've drove everyone out of path.. :P

the solution is pretty simple then.
you just have to add a snippet like this one, where you want to display the loop of custom posts


<?php
global $post;
$author_id = $post->post_author;

$custom_args = array(
"post_type" => "CUSTOM_POST_TYPE", //put here the name of your custom post type
"author" => $author_id,
"posts_per_page" => 4 // put here the number of posts you want to show (put -1 if you want to show them all)
);

$custom_query = new WP_Query($custom_args);

// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
// display what you want, by instance the title
?>

<p><?php the_title();?></p>

<?php
endwhile;
endif;

// Reset Post Data
wp_reset_postdata();
?>


this will pick the id of the author of the currently viewed post. and then it will query the posts from the CPT which have the same author.

remember to change "CUSTOM_POST_TYPE" and put the actual name of your CPT.
then I set the query to grab 4 posts, but you can change that too if you want


Andrea P comments:

now that I am thinking, I guess your aim is to have a BIO page for each author (which is within a custom post type), and then you want to display the author's BIO infos below each of the normal posts published by him?

just a quick tip for the future: when you seek help from developers, it is always better to explain what is the practical functionality that you want to achieve, using common languange, and then the developers will figure out which is the best way to approach this issue on a coding prospective. that is a great part of our everyday job indeed. ;)

cheers!


Andrea P comments:

uopss!

sorry I did a copy&paste mistake..

$the_query should be substituted by $custom_query in this example..

correct code is this:


<?php
global $post;
$author_id = $post->post_author;

$custom_args = array(
"post_type" => "CUSTOM_POST_TYPE", //put here the name of your custom post type
"author" => $author_id,
"posts_per_page" => 4 // put here the number of posts you want to show (put -1 if you want to show them all)
);

$custom_query = new WP_Query($custom_args);

// The Loop
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) : $custom_query->the_post();
// display what you want, by instance the title
?>

<p><?php the_title();?></p>

<?php
endwhile;
endif;

// Reset Post Data
wp_reset_postdata();
?>


pjeaje comments:

It only displays with the current author, I need it to display all the time.

2016-04-15

Reigel Gallarde answers:

how does one add it's CPT?


pjeaje comments:

Any way they want


pjeaje comments:

The way they add their CPT isn't important is it?


Reigel Gallarde comments:

I mean, what code that does this? we need a way to insert user information or something...


pjeaje comments:

just need to be able extract/display any data/info/meta from the CPT and put it into single.php


pjeaje comments:

I need the author's CPT loop to be inserted into the author's single.php template


Reigel Gallarde comments:

Sorry, my mistake, hmmm how is $authorid declared here? $args2=array('author'=>$authorid,'post_type'=>'school_profile', 'numberposts'=> -1);


pjeaje comments:

I have no idea, I just thought it may help


pjeaje comments:

This is a snippet from a different question...
http://wpquestions.com/question/showChronoLoggedIn/id/14245


Reigel Gallarde comments:

very well, this is just a try and let's see what will happen...

above this,

$args2=array('author'=>$authorid,'post_type'=>'school_profile', 'numberposts'=> -1);


add

$current_user = wp_get_current_user();
$authorid = $current_user->ID;


this code by the way is for current version of WP


Reigel Gallarde comments:

ooppss... I already posted the answer when I saw your latest update...

add this instead of those two lines..

$authorid = the_author_meta('ID');


pjeaje comments:

Only displays to the author, not the public and it only works outside the normal post loop. Can this be made to work inside the normal post loop?


Reigel Gallarde comments:

this one? $authorid = the_author_meta('ID');


pjeaje comments:

Only displays to the author, not the public and it displays all CPT posts


Reigel Gallarde comments:

I'm thinking we need to see your single.php.. your code example should work if it has the correct $authorid... what's lacking there is just the correct $authorid..

2016-04-15

Arnav Joy answers:

can you explain this point " Each normal post of each author has their CPT added into normal post " ?


pjeaje comments:

in single.php the author's CPT is also in this template


Arnav Joy comments:

you want to find out authors data in single.php ?


pjeaje comments:

No, not author's data, the author's CPT data


pjeaje comments:


single.php

normal post loop
CPT post loop

2016-04-15

Sébastien | French WordpressDesigner answers:

it's really confused as issue

I think that you write "CPT" insead of "CP"... no ?
In fact, each author has a post in a unique CPT. Is it that ?

For example, you have a CPT "my_authors", and each author has a post in this this CPT. Is it that ?


pjeaje comments:

CPT =a post form a custom post type


pjeaje comments:

CPT =a post from a custom post type


Sébastien | French WordpressDesigner comments:

<?php

$authorid = the_author_meta('ID');
$args2 = array(
'author'=>$authorid,
'post_type'=>'school_profile', //replace school_profile by the name of your CPT
'numberposts'=> 1
);

$cquery2 = new WP_Query($args2);

if($cquery2->have_posts()):

while($cquery2->have_posts()):

$cquery2->the_post();

?>
<p><?php the_title();?></p>
<?
endwhile;
wp_reset_postdata();
endif;

?>


Sébastien | French WordpressDesigner comments:

"a post from a custom post type" is not a custom post type.

For example you can have a custom post type "brands of cars" and have many posts in this CPT like "mercedes", "bmw" etc...

So, in my code, just replace school_profile by the name of your CPT (probably "the authors" or something like that in your case)


Sébastien | French WordpressDesigner comments:

this code diays only one post of this CPT for each autho
but if you want display all post of this CPT for ech author, replace 1 by -1


pjeaje comments:

It only displays with the current author, I need it to display to the public.


pjeaje comments:

ok it works