wp query – Add metabox if there is at least one post available
Question
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:
- Is there a way that I can invoke the
add_action
and thepardot_dashboard_widget()
function ONLY if there is at least one or more posts inget_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()
{
add_meta_box(
'pardot_dashboard_meta_box',
esc_html__( 'Pardot Form Locations', 'wporg' ),
'pardot_dashboard_stats',
'dashboard',
'side', 'high'
);
}
add_action('wp_dashboard_setup', 'pardot_dashboard_widget');
function 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 no active pardot forms available.';
}
}
0
11 months
2021-09-06T19:40:05-05:00
2021-09-06T19:40:05-05:00 0 Answers
0 views
0
Leave an answer