I’m trying to create a list of parent IDs for a custom post type for use within a “must use” plugin I’m developing on a project.
Here’s the code I’m using to generate the query:
$cpt_parents = new WP_Query( array (
'post_type' => 'cpt',
'posts_per_page' => -1, // Get them all
'post_parent' => 0, // Return only top-level
'fields' => 'ids'
)
);
$parent_ids = $cpt_parents->posts;
And here are the resulting errors:
Notice: Undefined index: wp_taxonomies in [url]/wp-includes/query.php on line 1700
Warning: Invalid argument supplied for foreach() in [url]/wp-includes/query.php on line 1700
Notice: Undefined index: wp_taxonomies in [url]/wp-includes/query.php on line 1700
Warning: Invalid argument supplied for foreach() in [url]/wp-includes/query.php on line 1700
Fatal error: Call to undefined function is_user_logged_in() in [url]/wp-includes/query.php on line 2485
What do I need to do in order to run WP_Query within a plugin?
I figured it out. I moved the reference to WP Alchemy within a function that fires after init. Everything now works great and I can use WP_Query within the setup files.
Here’s a code reference: