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, 7 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket

Tagged: ,

  • Author
    Posts
  • #427534

    Alex Howes
    Expired Member
    Post count: 175

    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 🙂

    #427535

    Alex Howes
    Expired Member
    Post count: 175

    Alternatively 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.

    #427578

    Guust
    Moderator
    Post count: 29970

    The 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 …

    #427593

    Alex Howes
    Expired Member
    Post count: 175

    Hmm. 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.

    #427680

    Kiran
    Moderator
    Post count: 7069

    Hi 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,
    Kiran

    #427716

    Alex Howes
    Expired Member
    Post count: 175

    Great, 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?

    #427731

    Alex Rollin
    Moderator
    Post count: 27815

    – Use filter ‘wp_insert_post_empty_content’ that is executed before insert post.

    #427809

    Alex Howes
    Expired Member
    Post count: 175

    Thanks 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;
    }

    #427819

    Kiran
    Moderator
    Post count: 7069

    Glad to hear that it worked for you.

    Thanks for sharing 🙂

    #427822

    Alex Rollin
    Moderator
    Post count: 27815

    That is a clever solution, I like it!

    #429310

    Alex Howes
    Expired Member
    Post count: 175

    Thanks! 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?

    #429341

    Kiran
    Moderator
    Post count: 7069

    Hi,

    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

    #429368

    Alex Howes
    Expired Member
    Post count: 175

    Hi,

    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

    #429384

    Alex Howes
    Expired Member
    Post count: 175

    Since 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;
    }
    #429473

    Kiran
    Moderator
    Post count: 7069
    This reply has been marked as private.
Viewing 15 posts - 1 through 15 (of 22 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
20% Discount Offer
Hurry! Get your 20% discount before it expires. Get 20% Discount