views:

146

answers:

2

Hey.

I am trying to make a plugin for wordpress that is supposed to use the function get_posts (declared in one of wordpress' include files, but depending on a lot of other files depending on more files.), and I just can't seem to find out how to do it!

Please help. Thank you :)

+1  A: 

You're most likely trying to call get_posts() before WordPress is actually ready (WP loads plugins before it loads other libraries).

Wrap your code in a function, and hook it to the init event;

function my_plugin_code()
{
    // run get_posts() and what you like here
}
add_action('init', 'my_plugin_code');
TheDeadMedic
The only thing is that it does not need to be executed unless someone calls for it. It won't be executed unless it has an action or filter attatched to it, will it? I'll still vote this up, I understand why it didn't work now. Thanks :)
Emil
All the action does is delay when `my_plugin_code` runs. You can choose to add the hook only under certain circumstances if you don't want it to run on every load.
TheDeadMedic
I'll accept this, because this is what most people should use in most cases. Thans @TheDeadMedic :)
Emil
A: 

Fixed it by including the wp-load.php-file.

Can be done in another way,

require_once('../../../wp-config.php');
require_once('../../../wp-includes/classes.php' );
require_once('../../../wp-includes/functions.php' );
require_once('../../../wp-includes/plugin.php' );

That's all the files you'll need.

Emil
Sorry Emil, but I'd highly advise you don't use that method. It's completely unnecessary and makes too many assumptions about the WordPress file hierarchy.Stick to using plugin standards,namely actions and filters as I mentioned in my answer.
TheDeadMedic
Why shouldn't I? Is this more resource-demanding than your solution?
Emil
The plugin will only be used by me, will not be distributed.
Emil
If it's just for you, then it's not really a problem - I just wondered why you'd want to re-invent the wheel, when WordPress gives you the wheel with chocolate sprinkles on top ;)
TheDeadMedic