Skip Navigation

[Resolved] Restrict number of directory listings per user

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to only show a Form to submit content if the user hasn't already submitted any (i.e. only allow them to submit one post)?

Solution:
You would need to register a custom shortcode to return the number of posts a user has published to use inside a wpv-conditional shortcode that determined whether the link to the form was displayed or not.

Here is an example of such code:

add_shortcode( 'user-posts', function( $atts = [], $content = null ){
 
    // default is any post type
    $atts = shortcode_atts( 
        array(
            'type'      =>   'any'
        ), 
        $atts
    );
 
    $user_id = get_current_user_id();
    $posts = get_posts( array( 'post_type' => $atts['type'], 'post_author' => $user_id, 'numberposts' => -1 ) );
 
    // Return number of published posts
    return count( $posts );
 
});

The custom shortcode needs registering at Toolset > Settings > Front-end content to be able to use it for conditions.

The shortcode can be used with or without an attribute to specify the post type, like so:

[user-posts] // Will output the number of posts of any type published by the current user
[user-posts type='thing'] // Will output number of posts of post type 'thing'

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/
https://toolset.com/documentation/adding-custom-code/

This support ticket is created 6 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by lucyD 6 years, 4 months ago.

Assisted by: Nigel.

Author
Posts
#1097875

I have built a directory site, mostly following your documentation (https://toolset.com/learn/build-directory-classifieds-sites-using-toolset/).

Everything is working well but I wondered whether it is possible to add the restriction that each user can only create a single directory listing.

What is the link to your site? hidden link

#1098442

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Lucy

I don't know the details of your site, but if your users are able to publish content using front-end forms and you want to limit them to be able to publish only a single post then you would use a conditional shortcode to only show the link to the form when they do not have any published posts.

That should get easier with an update to Views which is coming soon, but for now you'll need a custom shortcode to output how many posts are authored by the current user which you can use in a conditional shortcode.

Here is a sample shortcode you can use:

add_shortcode( 'user-posts', function( $atts = [], $content = null ){

	// default is any post type
	$atts = shortcode_atts( 
		array(
			'type'		=>	'any'
		), 
		$atts 
	);

	$user_id = get_current_user_id();
	$posts = get_posts( array( 'post_type' => $atts['type'], 'post_author' => $user_id, 'numberposts' => -1 ) );

	// Return number of published posts
	return count( $posts );

});

You'll need to add that to your theme's functions.php file.

[user-posts] // Will output the number of posts of any type published by the current user
[user-posts type='thing'] // Will output number of posts of post type 'thing'

After registering the shortcode at Toolset > Settings > Front-end Content you can then use it in conditional shortcodes, as described here: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/

#1103708

Resolved.