• I want to use Wp-Cron to trigger a plugin, and fectch and process some posts based in certain conditions. What is the recommended way to accomplish this “Loop” inside such plugin?

    I have been searching the Codex, and reading plugins source code around, but it looks like everyone takes a different approach for this.

    My point is, if I use something like the Loop inside my plugin I should be able to keep all other processing by other plugins, like custom Excerpts, etc. And I haven’t been able to set a Loop…

    Any help woul be hilghly appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The Loop is a WordPress term that refers to WordPress’ action of displaying posts. The Loop uses
    while ( have_posts() ) : the_post()
    to iterate through each post that satisfies the get_posts() conditions from the query string or blog settings.

    You can use Multiple Loops to do various things. Maybe that’s what you’re trying to accomplish?

    Give us more details about what you want, what you’ve tried, and what’s happened, please.

    Thread Starter anibalrojas

    (@anibalrojas)

    Skippy,

    Thanks for your response. Actually I am trying to modify your suscribe2 plugin in order to send a daily digest of recent posts. I tried:

    while ( have_posts() ) {
    the_post();

    setting a post_query before, but it looks like no posts are fectched. I could use raw SQL to recover the past day posts, that should be easy, but I suppose that’s not the right way to do it. I want to rely on the WP api, and not in SQL.

    The WordPress API is great, but not perfect. For a lot of things, it’s sometimes best to go right to the database.

    Calling while ( have_posts()) requires you to first setup which posts you want to collect. You can use the query_posts template tag to do this:

    <?php
    $this_month = date('m');
    $yesterday = (date('d') - 1);
    query_posts("monthnum=$this_month&day=$yesterday"); ?>
    <?php while (have_posts()) : the_post(); ?>

    Note: completely untested.

    Thread Starter anibalrojas

    (@anibalrojas)

    The code works, thanks for your help, som attributes are fetched other don’t.

    I went down to the source code, and I would not call the the WordPress API “great”, actually I am not sure there is an API at all.

    It looks like there is no way to recover the Post information in a consistent and ordered way, I am not sure if the code is a real mess or it is just I am not familiar enough with the PHP languaje…

    It looks like there is no way to recover the Post information in a consistent and ordered way

    What do you mean?

    Here’s the WordPress API way to fetch a post’s data
    <?php
    // fetch all the data for post ID #13
    $post_thirteen = get_post(13);
    echo $post_thirteen->post_title;
    ?>

    That’s a convenient wrapper around the direct SQL:
    <?php
    $post_thirteen = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID=13");
    echo $post_thirteen->post_title;
    ?>

    Using the API has the added benefit of using a post cache: if the post your requesting has previously been selected, it’s data will be cached and get_post() will avoid the unnecessary hit to your database.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Looping inside a plugin’ is closed to new replies.