Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling a Custom Post Type for Related Author Posts
    text
    copied!<p>Does anyone know how to call a custom post type (CPT) in the <code>get_related_author_posts()</code> query? </p> <p>I am using the function in my functions.php file and then calling it in a CPT template with <code>&lt;?php echo get_related_author_posts(); ?&gt;</code> so that links to other content and content types by the same author can appear in the template.</p> <p>All the research I have done suggests that I should be able to achieve what I want by adding the CPT query <code>'post_type' =&gt; 'webarticle'</code> to the function. </p> <p>Seems straightforward, right? </p> <p>But this is not working for me. Below is my code. Any tips on how to do this or where I am going wrong would be greatly appreciated. Thank you!</p> <pre><code>function get_related_author_posts() { global $authordata, $post; $authors_posts = get_posts( array( 'author' =&gt; $authordata-&gt;ID, 'post_type' =&gt; 'webarticle', 'post__not_in' =&gt; array( $post-&gt;ID ), 'posts_per_page' =&gt; 5 ) ); $output = ''; foreach ( $authors_posts as $authors_post ) { $output .= '&lt;p&gt;' . '&lt;a href="' . get_permalink( $authors_post-&gt;ID ) . '"&gt;' . apply_filters( 'the_title', $authors_post-&gt;post_title, $authors_post-&gt;ID ) . '&lt;/a&gt;' . '&lt;/p&gt;' ; } $output .= ''; return $output; } </code></pre> <p><strong>Here is new markup -- still not working -- with the <code>wp_reset_query()</code> function added.</strong> </p> <pre><code>function get_related_author_posts() { global $authordata, $post; $authors_posts = get_posts ( array( 'author' =&gt; $authordata-&gt;ID, 'post__not_in' =&gt; array( $post-&gt;ID ), 'posts_per_page' =&gt; 5 ) ); wp_reset_query() $args = array('post_type' =&gt; 'webarticle'); $query = new WP_Query( $args ); if ( $query-&gt;have_posts() ) { while ( $query-&gt;have_posts() ) { $query-&gt;the_post(); //code for each post from query// $output = '&lt;ul&gt;'; foreach ( $authors_posts as $authors_post ) { $output .= '&lt;li&gt;&lt;a href="' . get_permalink( $authors_post-&gt;ID ) . '"&gt;' . apply_filters( 'the_title', $authors_post-&gt;post_title, $authors_post-&gt;ID ) . '&lt;/a&gt;&lt;/li&gt;'; } $output .= '&lt;/ul&gt;'; return $output; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload