0

I want to use the following code in my main php plugin file. I get a whole screen full of error messages.

When I put it in functions.php it works fine. Can someone advise please?

$pages = get_posts($args);

foreach($pages as $page) {

    $out = get_permalink($page->ID);
}
3
  • Without knowing exactly what you have done, no one can tell you what you are doing wrong Commented Sep 16, 2015 at 19:55
  • You're passing in an array named $args, but you haven't included that in your question, can you edit your question to include the missing code?
    – Tom J Nowell
    Commented Sep 17, 2015 at 13:57
  • This is the complete code $args = array( 'orderby' => 'post_title', 'order' => 'ASC', 'post_type' => 'page', 'showposts' => 1000, 'caller_get_posts' => 1 ); $pages = get_posts($args); foreach($pages as $page) { $out = get_permalink($page->ID); }
    – I. Sher
    Commented Sep 18, 2015 at 18:11

1 Answer 1

0

You need to use a hook and a function in your plugin file. For example:

function sher_posts() {

    $pages = get_posts($args);

    foreach($pages as $page) {

        $out = get_permalink($page->ID);
    }
}
add_action('wp', 'sher_posts'); 

Of course this example doesn't know what $args are or what to do with $out.

1
  • while in general you should hook your code on an action or filter, I don't see any reason why should that code not work at plugin load time in the global context. Commented Sep 17, 2015 at 6:51

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.