wp query – Add metabox if there is at least one post available


So I am building a WordPress Dashboard widget which will showcase all posts & pages where a Gutenberg Block is active.

The below code does it’s job and pulls in an array based on get_posts().

Here is what I’m attempting to do:

  1. Is there a way that I can invoke the add_action and the pardot_dashboard_widget() function ONLY if there is at least one or more posts in get_posts()? If it’s an empty array, don’t even bother creating the metabox.

Here is the code:

 * Pardot Widget for Dashboard
function pardot_dashboard_widget()
        esc_html__( 'Pardot Form Locations', 'wporg' ),
        'side', 'high'
add_action('wp_dashboard_setup', 'pardot_dashboard_widget');

function pardot_dashboard_stats()
    $args = [
        's'         => '<!-- wp:acf/pardot-form ',
        'sentence'  => 1,
        'post_type' => [
    $pardot_form_query = get_posts($args);
    if (!$pardot_form_query) {
       echo 'There are no active pardot forms available.';

theMap 11 months 2021-09-06T19:40:05-05:00 0 Answers 0 views 0

Leave an answer