Condition – Current user has published at least one post (of specific post type)

Tony Nguyen
Tony Nguyen
April 13, 2024

This tutorial will show you how to display an element based on the number of published posts by the current user for a specific post type.

We are going to create a condition for Breakdance using its Element Display Conditions API.

Step 1

Copy this code and paste it into Code snippet plugin or similar one:

add_action(
    'breakdance_register_template_types_and_conditions',
    function() {
        \Breakdance\ConditionsAPI\register(
            [
                'supports' => ['element_display'],
                'slug' => 'unique-prefix-user-has-posts', // Ensure the slug is unique within your installation
                'label' => 'Current User Has At Least 1 Post',
                'category' => 'User',
                'operands' => ['equals'],
                'values' => function() {
                    return [
                        [
                            'label' => 'Has 1 and more posts',
                            'items' => [
                                [
                                    'text' => 'True',
                                    'value' => 'true'
                                ],
                                [
                                    'text' => 'False',
                                    'value' => 'false'
                                ]
                            ]
                        ],
                    ];
                },
                'callback' => function(string $operand, $value) {
                    $current_user_id = get_current_user_id();

                    // Handle cases where no user is logged in
                    if ($current_user_id == 0) {
                        return $value === 'false';
                    }

                    $args = [
                        'author' => $current_user_id,
                        'post_type' => 'post',
                        'post_status' => 'publish',
                        'fields' => 'ids', // Only get post IDs to speed up the query
                        'posts_per_page' => 1, // We only need to check if at least one post exists
                    ];

                    $query = new WP_Query($args);
                    $has_posts = $query->have_posts();

                    if ($operand === 'equals') {
                        if ($value === 'true') {
                            return $has_posts;
                        } else {
                            return !$has_posts;
                        }
                    }

                    return false;
                },
            ]
        );
    }
);

Make sure to change the post type to the desired post type you want to check; in my case, it is ‘post’.

Step1 1

Step 2

Go to the Breakdance builder. Select the element to which you want to apply this condition. Now, you have two options: true or false.

If it’s true, this means that if the current user has at least one published post, the element will be shown.

If it’s false, this means that if the current user has published zero posts, the element will not be shown.

Step 2

Important

The function get_current_user_id() works only for logged-in users. When a user is logged out, it will automatically return false because they will have no published posts at that moment.

Modification

If you need to consider more than 1 post type, you can modify the $args like this:

$args = [
    'author' => $current_user_id,
    'post_type' => ['post', 'page', 'custom_post_type'], // Add your desired post types here
    'post_status' => 'publish',
    'fields' => 'ids', // Only get post IDs to speed up the query
    'posts_per_page' => 1, // We only need to check if at least one post exists
];

You should replace ‘custom_post_type’ with the actual slug of any custom post types you wish to include in the check.

Headspin Logo
1.1.1
Built with
and
Headspinui
@ 2024 HeadspinUI. All rights reserved.