0

I create plugin to sent list post yesterday and today via wp_mail() to member from WordPress multisite, at localhost the result of list post delivery successful.

But at live server, the result of post displaying mix last month, last year post. Code

function test_plugin() {
$blogs = get_last_updated(' ', 0, 40);
global $wpdb;
//find today post from WordPress network
foreach ($blogs AS $blog) {    
    switch_to_blog($blog["blog_id"]);
    $today = getdate();
    $yesterday = date('F jS, Y',strtotime("-1 days"));
    $yesterday_today = array(
        'order' => 'ASC',
        'orderby' => 'date',
        'date_query' => array(
            array(
                'after'     => $yesterday, //'January 1st, 2013',
                'before'    => array(
                    'year'  => date('Y'),
                    'month' => date('n'),
                    'day'   => date('j'),
                ),
                'inclusive' => true,
            ),
        ),
        'posts_per_page' => -1,
    );

    $wpb_all_query = new WP_Query($yesterday_today); ?>
    <?php if ( $wpb_all_query->have_posts() ) : ?>
        <!-- the loop -->
        <?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); 
            $result = get_the_date('Y-m-d')." | ".get_bloginfo()." | ".'<a href="'.get_permalink().'?utm_source=newsletter&utm_medium=email&utm_campaign=budayasaya'.'">'.get_the_title()."</a><br>";
            $content[]=$result;
            ?>
        <?php endwhile; ?>
        <!-- end of the loop -->
        <?php wp_reset_postdata(); ?>
    <?php endif; ?>
    <?php restore_current_blog(); ?>
    <?php 
}
//save result as one variable
$list_post = implode(" ", $content);
//Check if any post, sent it
if ($list_post != NULL) {
    echo "This will sent email to users with content: <br>".$list_post;
    $subject = 'Daily News';
    $headers = array('Content-Type: text/html; charset=UTF-8');
//sent email to multiple recipients
    $get_email = $wpdb->get_results("SELECT email FROM penerima WHERE status = '1'");
    foreach ( $get_email as $recipients ) {
        $to = $recipients->email;
        wp_mail( $to, $subject, $list_post, $headers ); 
    }
}
}

My git https://github.com/ahmadbagwi/testemailblast/blob/master/index.php

  • Is there a reason why you're not using the same date format for yesterday and today (after and before)? Also it looks like you're not using $today variable anywhere... – D. Dan Sep 11 '18 at 14:48
  • It just try and error, I use $today before and now use $yesterday, $yesterday_today. The result same give me mix result on email body when sending with live server – ahmadbagwi Sep 13 '18 at 1:57
0

Can you try using another date format? Example:

foreach ($blogs AS $blog) {    
    switch_to_blog($blog["blog_id"]);
    $yesterday_today = array(
        'order' => 'ASC',
        'orderby' => 'date',
        'date_query' => array(
            array(
                'after'    => array(
                    'year'  => date('Y',strtotime("-1 days")),
                    'month' => date('n',strtotime("-1 days")),
                    'day'   => date('j',strtotime("-1 days")),
                ),
                'before'    => array(
                    'year'  => date('Y'),
                    'month' => date('n'),
                    'day'   => date('j'),
                ),
                'inclusive' => true,
            ),
        ),
        'posts_per_page' => -1,
    );
    //....
| improve this answer | |

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.