• i have a page in this page i use wp_query , this query run only with admin , the result of query is different base run query Date
    i want using wp_mail function for submit result query to my email
    how can insert query result in body of email?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    How you want the email to appear affects what code you would use. If a raw data dump is adequate, something like this might work (on your page template):

    $args = array( /*Your custom query args here*/ );
    $query = new WP_Query( $args );
    wp_mail( 'mymail@example.com', 'Query Result', print_r( $query, true ));

    If all you wanted was the posts found, not the entire query object, print_r $query->posts instead of just $query.

    If you want the email to look more like a webpage, you could collect the output from the loop using PHP’s output buffer, then email the buffer content. For this to work the content will need all applicable CSS included in a style block, the usual external CSS will not come through in emails.

    You also need to set the MIME type to ‘text/html’ with the ‘wp_mail_content_type’ filter for the email to appear like a webpage.

    You might want to add a conditional to send the email only when the current user is yourself, or else you’ll get email anytime anyone requests the page, including search bots.

    Thread Starter salar2016

    (@salar2016)

    this is my code

    array(
    
    'key' => 'janer',
    
    'value'   => 'drama',
    
    'compare' => '==',
    
    ),
    
        ),
    
    'tax_query' => array(
    
              'relation' => 'AND',
    
    	   array(
    
    	     'taxonomy' => 'age',
    
    	     'terms' => $slg1,
    
    	     'field' => 'slug'
    
    	   ),
    
    	   array(
    
    	     'taxonomy' => 'publisher',
    
    	     'terms' => $wslg1,
    
    	     'field' => 'slug'
    
    	   ),
    
    	)
    
          ); 			
    
    $query = new WP_Query( $args );
    ?>
    
      <?php while ( $query->have_posts()) : $query->the_post();
    
               	$datetime = strtotime($post->post_date);
    
    if( $datetime < ( time() - (60 * 60 * 24 * 200 ) ) ) { ?>
    
    • <?php the_title(); ?>
    • <?php } endwhile; wp_mail( 'myemail@gmail.com', 'Query Result', print_r( $query->posts, true )); ?>

    i want email only title of post founded in thsi query then email result to my email
    thank you!

    Moderator bcworkz

    (@bcworkz)

    Before the while loop, initiate a variable within which we can collect titles for the email.
    $titles = "";

    Within the loop collect the titles, instead of or in addition to the_title(); :
    $titles .= get_the_title() . "\r\n"; //remove '\r' if your email's titles are double spaced and you don't like that.

    Then email the result. Change our wp_mail() line to this:
    wp_mail( 'myemail@gmail.com', 'Query Result', $titles );

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘mail result query in specific page to my email ?’ is closed to new replies.