Limit listings of certain CPTs to 1 per user
This topic contains 21 replies, has 5 voices, and was last updated by Alex Howes 6 years, 6 months ago.
We have moved to a support ticketing system and our forums are now closed.
Open Support TicketTagged: Add Listing, CPT
-
AuthorPosts
-
April 22, 2018 at 12:10 pm #427534
Hello
I’d like to only ever allow users to submit 1 listing of a certain CPT. I realise that this is not a feature of the core plugin, and GD has suggested using 3rd party plugins such as Bainternet to do this. However looking around the GD support forums it seems these don’t work with GD.
Is there another way to do this that you might have come across?
Thanks 🙂
April 22, 2018 at 12:23 pm #427535Alternatively it would be fine to make the old listing expire if a user creates a new one, if there was a way to do this.
April 22, 2018 at 9:29 pm #427578The only way I can see this work without customization is to
1. set the price of a listing at 1 million dollars or something like that
2. Create a 100% discount code that can only be used once by each user.But nothing can stop people creating multiple user accounts …
April 22, 2018 at 11:33 pm #427593Hmm. I realise users can create multiple accounts but that takes more effort on their part which I hope will be a discouragement!
I was thinking about the problem and wondering whether it’d be possible to create a user role for users who have submitted a listing, and assign users to this role when they submit a listing. It’d be pretty straight forward then to hide content from this role, e.g. the add listing page.
This would require some customization but perhaps only a small snippet, i.e. to hook into the listing submission action and then to reassign the user’s role. I think I can reassign the role using the WP_User class, but what would be the correct action/ filter to hook into the listing submission action (of a certain CPT)?
I understand that your support doesn’t cover customization but I’d be really grateful if you could point me in the right direction.
April 23, 2018 at 12:47 pm #427680Hi Alex,
I checked plugin https://wordpress.org/plugins/bainternet-posts-creation-limits/ but it not fuctioning well.
But you can refer the code and it will help you write your own code.
– Create one function that counts posts for author and post type.
– Use filter ‘wp_insert_post_empty_content’ that is executed before insert post.
– Via wp_insert_post_empty_content you can execute code that check author and post type limits. If author has already added the posts then you can add restriction here.In other way you can restrict add listing page from the user if author has already added post for that post type.
Thanks,
KiranApril 23, 2018 at 7:09 pm #427716Great, thanks! 🙂 I’ll have a look through that and see what I can do.
I’m interested in changing the user role when a user submits a listing, since I think that could be a simple solution. Do you know what geodirectory function/ action/ filter is relevant to hook into that?
I.e. I want to say “when a user submits a listing, change user role.” I know how to do the latter but not the former. Any tips?
April 23, 2018 at 8:47 pm #427731– Use filter ‘wp_insert_post_empty_content’ that is executed before insert post.
April 24, 2018 at 11:11 am #427809Thanks Kiran and Alex. I’ve now got a snippet working which changes user role when a worker submits a listing (in my case a CV listing). This then lets me easily hide content from the new role, such as the page to add a new listing. I’ll post my code here in case anybody else is interested in this.
add_filter(‘wp_insert_post_empty_content’, ‘change_user_role_when_submit_cv’);
function change_user_role_when_submit_cv( $data ) {
global $post;
$post_type = get_post_type( $post->ID );if ($post_type = ‘gd_cv’) {
$current_user = wp_get_current_user();
$current_user_roles = $current_user->roles;if (in_array(‘um_worker’, $current_user_roles)){
$current_user->set_role( ‘worker_has_cv’ );
}
}
return $data;
}April 24, 2018 at 11:39 am #427819Glad to hear that it worked for you.
Thanks for sharing 🙂
April 24, 2018 at 11:47 am #427822That is a clever solution, I like it!
May 6, 2018 at 5:39 pm #429310Thanks! Unfortunately I noticed a mistake. The first if statement should be:
if ($post->post_type ==’gd_cv’)
Now it isn’t working and I can’t work out why. Any ideas? Does $post here not refer to the listing form?
May 7, 2018 at 5:44 am #429341Hi,
Make sure you have correct single quotes within code.
with correct single quote:
if ($post->post_type == 'gd_cv')
with incorrect single quote:
if ($post->post_type == ‘gd_cv’)Kiran
May 7, 2018 at 9:30 am #429368Hi,
I see in my message before I used the wrong quotes, but in my code they’re the right ones. Something else must be the problem.
Alex
May 7, 2018 at 12:04 pm #429384Since the other parts of the code were working fine when the first “if” statement was working, it must be a problem with $post and how I’m trying to access the post type. I’ve put this bit in because I only want the user role to be changed when a listing of a certain CPT (CV) is listed.
add_filter('wp_insert_post_empty_content', 'change_user_role_when_submit_cv'); function change_user_role_when_submit_cv( $data ) { global $post; if ($post['post_type'] =='gd_cv') { $current_user = wp_get_current_user(); $current_user_roles = $current_user->roles; if (in_array('um_worker', $current_user_roles)){ $current_user->set_role( 'worker_has_cv' ); } } return $data; }
May 8, 2018 at 6:36 am #429473This reply has been marked as private. -
AuthorPosts
We have moved to a support ticketing system and our forums are now closed.
Open Support Ticket