NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

Intermediate

---- Last modified : Sep 11 2022

Estimated reading time : 2 minutes, 2 seconds - 191 words

With this tutorial we will do a Query Post type for each WordPress author. In the example below, our Post Type will be events. You need to give access to your authors via the Events Formidable Forms.

First thing we need to do is create a new template: tpl-evenements.php.

Query Post type by WordPress Authors

Make sure to create their identity inside the template. The user will need to be logged in their WordPress account.

<?php /* Template Name: Les Evenements */ ?>

Afterwards, feel free to add it in your header, footer,… .
Here’s the query you’ll need contained with your content DIV in order see the Events by authors.

<?php 
$author_id = get_current_user_id(); 
$args = array( 
'posts_per_page'            => 5,  // Limit to 5 posts
 'post_type'                 => 'evenements',  // Query for the default Post type
 'author'                    => $author_id  
    );
 $last_five_posts = get_posts( $args );
 foreach ( $last_five_posts as $post ) : setup_postdata( $post ); ?>
     
/////////////////////// Ici le contenu des résultats a afficher title, content, category, ect... //////////////////////////
     
<?php endforeach; wp_reset_postdata(); ?>

It is vital to close the query with the wp_reset_postdata(); function. Log into your WordPress account then create a new Events page and assign it to your Events template.

The Query Post type by WordPress authors will display properly when your users are connected.

Now we will twaek the query. If you want to display the author’s post type when clicking on an author link and display your author’s posts, you need to replace get_current_user_id(); with $post->post_author;

<?php 
$author_id = $post-&gt;post_author;
$args = array(
'posts_per_page'         => 5,  // Limit to 5 posts
'post_type'              => 'evenements',  // Query for the default Post type
'author'                 => $author_id	
);
$last_five_posts = get_posts( $args );
foreach ( $last_five_posts as $post ) : setup_postdata( $post ); ?>
 
/////////////////////// Ici le contenu des résultats a afficher title, content, category, ect... //////////////////////////
 
<?php endforeach; wp_reset_postdata(); ?>

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>