Site icon Hip-Hop Website Design and Development

Add metabox if there’s no less than one publish out there

So I’m constructing a WordPress Dashboard widget which can showcase all posts & pages the place a Gutenberg Block is energetic.

The beneath code does it is job and pulls in an array based mostly on get_posts().

Here’s what I am trying to do:

  1. Is there a approach that I can invoke the add_action and the pardot_dashboard_widget() operate ONLY if there’s no less than a number of posts in get_posts()? If it is an empty array, do not even hassle creating the metabox.

Right here is the code:

/**
 * Pardot Widget for Dashboard
 */
operate pardot_dashboard_widget()
{
    add_meta_box(
        'pardot_dashboard_meta_box',
        esc_html__( 'Pardot Kind Places', 'wporg' ),
        'pardot_dashboard_stats',
        'dashboard',
        'aspect', 'excessive'
    );
}
add_action('wp_dashboard_setup', 'pardot_dashboard_widget');

operate pardot_dashboard_stats()
{
    $args = [
        's'         => '<!-- wp:acf/pardot-form ',
        'sentence'  => 1,
        'post_type' => [
            'post',
            'page'
        ],
    ];
    $pardot_form_query = get_posts($args);
    if (!$pardot_form_query) {
       echo 'There are not any energetic pardot kinds out there.';
    }
}