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);
}
  • Without knowing exactly what you have done, no one can tell you what you are doing wrong – Pieter Goosen Sep 16 '15 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 Sep 17 '15 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 Sep 18 '15 at 18:11
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.

  • 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. – Mark Kaplun Sep 17 '15 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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