Skip Navigation

[Closed] Limit the number of custom posts to one and implement solution within the menu

This support ticket is created 4 years, 7 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 9 replies, has 2 voices.

Last updated by Jamal 4 years, 7 months ago.

Assisted by: Jamal.

Author
Posts
#1582381

Hi,

I am looking for a simple solution for some of the available custom posts that I have created, where the need is to:

1. define a limitation for the number of custom posts that can be published. The idea here is to have just one custom post per registered user

2. to implement this solution with links in the main navigation menu. So when user logs he/she sees a link in the menu to post a custom post, and after the post is published, this link in the menu becomes hidden and replaced with another link in the same navigation menu to edit the already published custom post.

Best regards,

Petar

#1582413

Hello Petar and thank you for contacting the Toolset support.

Toolset does not act on regular WordPress menus. I'll need more details about your menu to better advise you.

We will need a function in the theme's functions.php file that counts the number of posts for the logged-in user:

function my_u_post_content() {
    $user_post_count = count( get_posts( array(
        'post_type' => 'post-type-slug', // <== put your post type slug here
        'author' => get_current_user_id(),
    ) ) );
    return $user_post_count;
}

Now, you have a way to count the user created post on that custom post type, you can use it on validation or on conditional or on a custom function that adds a specific link to the menu.
Check this article that adds "Login/Logout" link to the menu programmatically
hidden link

I hope this helps. Let me know if you have any questions.

#1582507

I am creating a site where every action by user is done via dedicated page. For example:
- user logs on created login page via embedded login form
- user creates a user registration via registration form embedded in registration page
- user post a custom post via post form embedded in publish custom post page
ans so on. You get the concept.

Now it is time to create a page for editing the custom posts:
- I will have situation when a user can have a lot of specific custom posts and has to be able to edit all those posts
- I will have different situation for different custom post, when a user must have only one custom post and have to edit that custom post on a dedicated page

The challenge here is how to:
- limit the creation of specific custom post (the user creates a new custom post by clicking a create new custom post link at the navigation menu, a page loads with embedded post form and custom post form is submitted). So after the first publishing of the specific custom post to hide the link in the menu to the page for creating custom post
- how to have an edit custom post page with embedded edit custom post form, in a way that when the edit custom post page is loaded to have directly loaded edit custom post form (without intermediate steps like click on link on the loaded page - the post edit link approach)

BR,

Petar

#1583105

Hello Petar,

For support rules, we are able to handle only one issue at the time. This helps us to bring you a better service and also helps other users to find all the information here exposed.

Let's focus on this ticket about the original request and we can create another ticket for any other request.

Acting on WordPress menus is out of the scope of all Toolset Plugins. To modify a WordPress plugin, I suggest one of the following solutions:
- Custom code: similar to this article hidden link
- Using a 3rd party plugin: This plugin seems a good fit https://wordpress.org/plugins/if-menu/
Both these solutions are out of the scope of the Toolset Support forum, and If you are not familiar with website development, you should consider hiring a developer or one of our partners https://toolset.com/contractors/

Toolset, on the other hand, gives several ways to get what you try to have. I'll explain below and example of a "Toolset Training" website. The user can create only one Profile.

1. Create custom post type Profile.
2. Create custom fields for the Profile custom post.
3. Create a Toolset form for creating a Profile (Create Profile).
4. Create a Toolset form for editing a Profile (Edit Profile).
5. Create a page "My Profile", where we will show the "Create Profile" form or the "Edit Profile" depending on whether the user has created a profile or not.

At this stage, Toolset has no built-in way to limit the user to only one profile post. We will create a function on the theme's code to use withing Toolset.
6. Add the following code to your theme's functions.php file. Adapt the code depending on your custom-post-type slug, here we use "profile".

function my_user_profile_count() {
    $user_post_count = count( get_posts( array(
        'post_type' => 'profile', // <== put your post type slug here
        'author' => get_current_user_id(),
        'post_status' => array('publish', 'draft', 'pending', 'private'),
    ) ) );

    return $user_post_count;
}

Note that in post_status, we are searching for several statuses, you can adapt that to your use case too.

7. Adding the functions to Toolset->Settings->Front-end Content->Functions inside conditional evaluations.

8. On the "My Profile" page. Assuming you are using Gutenberg.
8.1. Add a Toolset conditional block, on the condition use custom function "my_user_profile_count" equals to static value 0.
8.1.1. Check this screenshot hidden link
8.1.2. Add a form block inside the conditional block and choose the "Create Profile" form.

8.2. Add a Toolset conditional block, on the condition use custom function "my_user_profile_count" greater than static value 0.
8.2.1. Check this screenshot hidden link
8.2.2. Add a View Block inside the conditional block. The view should show the Profile posts whos the author is the current user, and the status is any of "publish", "pending", "draft", "private". Check this screenshot hidden link
8.2.3. Insert a Toolset form block inside the view loop. Choose the "Edit Profile" form. And the "Post to edit" is "The current post". Check hidden link
8.3. Save the page.
Check what I got too far. hidden link

If you are using the legacy Toolset Views editor, read more about it and check the images here https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/

Let's get back to the menu, I was able to update the menu by adding a custom visibility rule to the "If Menu" plugin based on our custom function to count Profile posts. Visibility rules are described on the faqs section of the plugin page https://wordpress.org/plugins/if-menu/
Add the following code to your theme functions.php file:

add_filter('if_menu_conditions', 'my_new_menu_conditions');

function my_new_menu_conditions($conditions) {
  $conditions[] = array(
    'id'        =>  'my-user-has-profile-post',                       // unique ID for the rule
    'name'      =>  __('User has Profile', 'i18n-domain'),    // name of the rule
    'condition' =>  function($item) {                                   // callback - must return Boolean
      return my_user_profile_count() > 0;
    }
  );

  return $conditions;
}

Then configure the rules on your WordPress menu:
- Check my settings on this screenshot hidden link
- Check my results. hidden link

I hope this answers your original question. Let me know if you have any doubts.

#1583393

Hi,

I have followed all of the defined above steps, but:
- at 8.2.2. the view shows "no items found", so there is no option for applying filters
- at the front-end of the site for logged user with successful registration, the profile page loads "Archives:..." and "It seems we can’t find what you’re looking for. Perhaps searching can help."

BR,

P

#1584927

Hello Petar,

For (8.2.2), I believe you are selecting the loop block(child of the view block), check this screenshot, it displays how to select the view block from the block hierarchy, then you will be able to set query filters. hidden link

For the second issue, I believe, that the page "Profile" has the same slug(profile) as the Custom Post Type "Profile", change the slug of the page to (my-profile) and it should fix the issue.
When WordPress encounters the slug of a post-type, it renders the archive page of that post-type. It has priority over checking the slugs for pages or posts. Does it make sense?

Best regards,
Jamal

#1585803
Capture.PNG

Wow! Great! It worked 🙂

One final touch - there is digit/number "1" before the first field in the custom post loaded in a profile page. How to remove that?

#1585815

I think that you have chosen an ordered list for the view. You will need to edit the view setting and use unformatted content.

I am not sure if you are using blocks for the view or the legacy view editor, please take a screenshot of your view and I will reply with a screenshot of when to modify the style of the view.

#1585919

Well, the joy was short. I recreated the view and now it does not load any data.

#1586595

Oh! I am sorry about that. we'll find a way to fix it 🙂

Would it be possible to allow me temporary access to your website for close verification? Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

The topic ‘[Closed] Limit the number of custom posts to one and implement solution within the menu’ is closed to new replies.