USP Pro version 2.3 introduces a new shortcode to display a list of submitted posts for any user and for any post type. This post explains how it works and how to customize the shortcode’s attributes to suit your needs.
Meet the Shortcode
To display a list of submitted post titles, add the following Shortcode to any WP Post, Page, or Custom Post Type:
[usp_pro_display_posts]
Customizing
The [usp_pro_display_posts]
Shortcode provides several attributes that enable you to customize the list of submitted posts:
userid specifies the user(s) for which posts should be displayed
all : display submitted posts from all users (default)
logged : display submitted posts from current logged-in user
current : display submitted posts from author of current archive page
Author Name : display submitted posts from specified USP Author Name (value of "usp-author" Custom Field)
Author ID : display submitted posts from specified WP Author ID (e.g., 1, 2, 3, etc.)
numposts specifies the number of posts to display
integer : displays the specified number of submitted posts (default: 10)
display specifies how the posts should be displayed
title : displays a list of submitted post titles (default)
content : displays the submitted post title and content
excerpt : displays the submitted post title and excerpt
thumb : displays only the featured image
content+thumb : displays title, content, and featured image
excerpt+thumb : displays title, excerpt, and featured image
title+thumb : displays title and featured image
loggedin specifies whether to display the posts for logged-in users
false : display submitted posts for all users (default)
true : display submitted posts only if user is logged in
status specifies the post status for which posts should be displayed
publish : display only published submitted posts (default)
pending : display only pending submitted posts
draft : display only draft submitted posts
etc.. : display any valid Post Status: https://bit.ly/3eavb9u
modlinks specifies whether to display moderation links for each post
false : do not display "edit" and "delete" links for the post (default)
true : display "edit" and "delete" links for the post
formid specifies a specific form for which posts should be displayed
integer : limits displayed posts to a specific USP Form (no default)
cat specifies the category(ies) for which posts should be displayed
integer : limits displayed posts to a specific category (e.g., 2) (no default)
integers : limits displayed posts to multiple categories (e.g., 2,6,17,38)
current : limits displayed posts to current category/categories
order specifies the order in which posts should be displayed
DESC : orders posts in descending order (default)
ASC : orders posts in ascending order
orderby specifies the order in which posts should be displayed
param : orders posts by any valid parameter (e.g., ID, author, title, et al) (default = date)
post_type specifies the post type for which posts should be displayed
any valid post type or 'any' for any post type (default = any)
post_link specifies the URL to use for linked post titles
permalink : use the post permalink/URL (default)
user_url : use the user-submitted URL (if available)
link_title specifies whether to link the post titles
true : link post titles (default)
false : do not link post titles
You can find more info about these parameters at the WP Codex.
Examples
Here are some examples showing different shortcode configurations:
[usp_pro_display_posts userid="1"] : displays all submitted posts by registered user with ID = 1
[usp_pro_display_posts userid="current"] : displays all submitted posts by author of current archive page
[usp_pro_display_posts status="pending" userid="logged"] : displays all pending submitted posts from the current logged-in user
[usp_pro_display_posts userid="Pat Smith"] : displays all submitted posts by author name "Pat Smith"
[usp_pro_display_posts loggedin="true"] : displays all submitted posts only to logged-in users
[usp_pro_display_posts userid="all"] : displays all submitted posts by all users/authors
[usp_pro_display_posts numposts="5"] : displays 5 most recent submitted posts
[usp_pro_display_posts display="excerpt"] : display excerpts of submitted posts
As a bonus, the [usp_pro_display_posts]
Shortcode displays any posts that were submitted by the free version of USP.
Theme Template
Just a tip: you can display any shortcode directly in your theme template using WP’s do_shortcode() function. So to display a list of user-submitted posts, you can do something like this:
<?php echo do_shortcode('[usp_pro_display_posts]'); ?>
And then flesh it out with any necessary shortcode attributes.
Display Multiple Post Types
The [usp_pro_display_posts]
shortcode makes use of WP’s get_posts()
function. And for that function, the post_type
parameter accepts either a string (for single CPT) or array (for multiple CPTs). So if using the get_posts()
function directly, you can specify multiple post types by using an array of values.
Unfortunately, it is not possible to pass an array to get_posts()
when using the [usp_pro_display_posts]
shortcode. Why? Because with shortcodes, you can pass only string values. So that means with the shortcode you can specify only one post type.
But no worries, USP Pro has got you covered. To display multiple post types simply add this snippet via your theme (or child theme) functions.php
file, or add via simple custom plugin:
/*
display multiple post types with [usp_pro_display_posts]
more infos @ https://plugin-planet.com/usp-pro-display-list-submitted-posts/
*/
function usp_display_posts_args($args) {
if (isset($args['post_type'])) $args['post_type'] = array('post', 'page', 'movie', 'book'); // specify your post types here
return $args;
}
add_filter('usp_display_posts_args', 'usp_display_posts_args');
What is this code doing? It filters the get_posts()
argument that’s used by the shortcode. So instead of passing the post_type
string via the shortcode, the above snippet tells the plugin to display whichever post types are specified in the array. Notice in the code snippet where it says array('post', 'page', 'movie', 'book')
. That is just an example. You can change/edit those values to match whichever post types you want to display.
Display Posts from Specific Dates
Another example of customizing the post query for the [usp_pro_display_posts]
shortcode. Here we are specifying a range of dates from which posts are displayed.
/*
display posts from specific date range with [usp_pro_display_posts]
more infos @ https://plugin-planet.com/usp-pro-display-list-submitted-posts/
*/
function usp_display_posts_args($args) {
$args['date_query'] = array(
array(
'year' => 2020,
'month' => 12,
'day' => 20,
),
);
return $args;
}
add_filter('usp_display_posts_args', 'usp_display_posts_args');
This works in the same way as the previous “post types” example. By customizing the date_query
argument, you can display posts from literally any date range. Learn more about available parameters via the resources linked below.
Learn more about the related WordPress functionality:
Display Posts with Specific Tags
Yet another example of customizing the post query for the [usp_pro_display_posts]
shortcode. Here we are specifying a specific tag from which posts are displayed.
/*
display posts with specific tags with [usp_pro_display_posts]
more infos @ https://plugin-planet.com/usp-pro-display-list-submitted-posts/
*/
function usp_display_posts_args($args) {
$args['tag'] = 'YOURTAGNAME';
}
add_filter('usp_display_posts_args', 'usp_display_posts_args');
Should do the trick. Just change YOURTAGNAME
to your actual tag name/slug. Save changes and done.
Customize the “No posts found” message
By default when no submitted posts are displayed by [usp_pro_display_posts]
, a message is displayed that says, “No posts found”. To change it to say something else, you can customize by tapping into a filter hook. To do so, add the following code snippet to your WordPress site:
/*
customize the "no posts found" message for [usp_pro_display_posts]
more infos @ https://plugin-planet.com/usp-pro-display-list-submitted-posts/
*/
function usp_display_posts_noposts($message) {
return '<p>'. esc_html__('No posts found', 'usp-pro') .'</p>';
}
add_filter('usp_display_posts_noposts', 'usp_display_posts_noposts');
No changes are required, but you’ll probably want to change the No posts found
text to whatever you would like to display. Once everything is ready, upload to your site and done.
Advanced Customization
For advanced customization, the following filter hooks are provided:
usp_display_posts_default
usp_display_posts_args
usp_display_posts_noposts
usp_shortcode_display_posts_size
usp_shortcode_display_posts_title
usp_shortcode_display_posts_edit_title
usp_shortcode_display_posts_edit_text
usp_shortcode_display_posts_delete_title
usp_shortcode_display_posts_delete_text
usp_shortcode_display_posts_modlink_sep1
usp_shortcode_display_posts_modlink_sep2
usp_shortcode_display_posts_before
usp_shortcode_display_posts_after
Check out the source code in /inc/usp-functions.php
for more information.