1

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?

5
  • I think it may have something to do with this issue: core.trac.wordpress.org/ticket/20904 Nov 12, 2012 at 20:37
  • Is that code alone in the plugin? Or inside a hook callback? If so, please provide the context...
    – brasofilo
    Nov 12, 2012 at 20:38
  • That code is alone in the plugin. I've also tried it within a function tied to the "init" action. It works great on the front-end, but throws the same errors on the backend. I'm pretty sure it's because the mu-plugin is being run prior to init, which appears to be a requisite, so far, for WP_Query. Nov 12, 2012 at 20:43
  • I'm really not getting it. Why would you run some unhooked code like this? Where exactly do you need it to run? Does this references (1 - 2) shred some light?
    – brasofilo
    Nov 12, 2012 at 22:03
  • I'm using WPAlchemy to create an interface that appears only on the "parent" of a custom post type. WPAlchemy has an include function that accepts an array of IDs. I'm trying to use the query above to generate that list of IDs. The problem is that all of this is run before "init" is called, which is preventing WP_Query from running properly. Nov 12, 2012 at 22:53

1 Answer 1

1

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:

// WP Alchemy Setup
function jd_setup_wp_alchemy() {
    include_once WPMU_PLUGIN_DIR . '/jonathanwold/metaboxes/setup.php';
    include_once WPMU_PLUGIN_DIR . '/jonathanwold/metaboxes/cpt-spec.php';
}
add_action('init', 'jd_setup_wp_alchemy');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.