write a query to get all published items from the custom post type doctors

WordPress AI Code Robot

Create WordPress code snippets for free without a developer!

neil requested the  following code from my WordPress AI Code Generator.

write a query to get all published items from the custom post type doctors

The robot generated the following code:

Here is a WordPress code snippet you may use to get all published items of custom post type 'doctors'. As requested, all functions are prefixed with 'nm_':

“`PHP
function nm_get_published_doctors() {
$args = array(
'post_type' => 'doctors',
'post_status' => 'publish',
'posts_per_page' => -1,
);

$query = new WP_Query($args);

if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();

// Display or return the post data here
echo '<h2>' . get_the_title() . '</h2>';
}
} else {
echo 'No doctors found';
}

// Reset Post Data
wp_reset_postdata();
}
add_action('init', 'nm_get_published_doctors');
“`

Please note that what you want to do with the post data is up to you, currently, it's just echoing the post's title. You could also return an array of data, store it in a variable, or further manipulate it as necessary. You would also typically call this function somewhere in your theme or plugin files, not add it as a 'init' action.

More
CODE SNIPPETS