Get All WP Posts older than 15 minutes - php

I would like to filter the WP_Query to retrive all woocommerce order (post_type = shop_order) with particular status that are older than 15 minutes.
I.e. All posts that have been last modified on or before (now - 15 minutes ) // Hope I'm clear.
Below What I've tried
function filter_where($where = '') {
//posts in the last 15 minutes
$where .= " AND post_modified > '" . date('Y-m-d', strtotime('INTERVAL 15 MINUTE')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
$args = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'shop_order_status',
'field' => 'slug',
'terms' => array('completed')
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$order_id = $loop->post->ID;
$order = new WC_Order($order_id);
print_r("<pre>");
print_r($order);
print_r("</pre>");
endwhile;
However that returns all record, Seems like have to modify the $where query, how can i achieve that ?

Do you need to filter the posts_where? Can't you just use date query parameters? In your case, specifically before.
$args = array(
'date_query' => array(
array(
'before' => '15 minutes ago'
'inclusive' => true,
),
),
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
I can't test this right now, so can't verify if it would work. If you are modifying an archive then you should definitely adjust the query via pre_get_posts in lieu of creating new query.

try this change date('Y-m-d', strtotime('INTERVAL 15 MINUTE')) to date('Y-m-d H:i:s', strtotime('-15 minutes'))
function filter_where($where = ''){
//posts in the last 15 minutes
$where .= " AND post_modified > '" . date('Y-m-d H:i:s', strtotime('-15 minutes')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');

Related

display most commented posts last month wordpress

I am trying to display most commented posts of certain category last month.
This is my code for now, I cant figure out what is wrong here, any ideas?
<?php
function filter_where( $where = '' ) {
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$the_query = new WP_Query(array( 'posts_per_page' => 3, 'cat' => 2, 'orderby' => 'comment_count date', 'order'=> 'DESC' ));
remove_filter( 'posts_where', 'filter_where' );
?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
echo get_the_title();
// and rest of content
endwhile;
wp_reset_postdata(); ?>
edit: p.s. btw I am using Vkontakte Api plugin for my comments. May be the problem is here, because this code actually works fine on other site. But, get_comments_number() shows the correct number, why then orderby => comment_count doesnt work?
You have to use date_query, for this.
$args = [
'posts_per_page' => 3,
'post_type' => 'post',
'date_query' => [
[
'year' => date('Y', strtotime(date('Y-m-d') . " -1 month")),
'month' => date('m', strtotime(date('Y-m-d') . " -1 month"))
]
],
'orderby' => 'comment_count',
'order' => 'DESC'
];
$posts = new WP_Query($args);
//$posts = get_posts($args);
//print_r($posts);
The MySQL query will be for getting the popular post of last month will be: (assuming NOW() = 13 Feb 2017)
SELECT
posts.ID,
posts.post_title,
posts.post_date
FROM
wp_posts AS posts
WHERE
YEAR (posts.post_date) = 2017
AND MONTH (posts.post_date) = 1
AND posts.post_type = 'post'
AND posts.post_status = 'publish'
ORDER BY
posts.comment_count DESC
LIMIT 0, 3;
Hope this helps!

WordPress running two queries together

This is an events website that handles the location of each event relative to the users location and displays the distance in search results.
I'm using the WP GeoQuery extension to allow me to query based on distance. However, it doesn't work well with things like tax_query. So, I'm wondering if there's a way of running the WP_GeoQuery to query for events within a certain distance, then run the result of that through the normal Wp_Query to get it to filter for the correct taxonomies and other arguments?
Here's the Geo Query:
$url = "http://freegeoip.net/json/". $_SERVER['REMOTE_ADDR'] .'';
$geo = json_decode(file_get_contents($url), true);
$geo_query = new WP_GeoQuery(array(
// location stuff
'latitude' => $geo[latitude], // User's Latitude (optional)
'longitude' => $geo[longitude], // User's Longitude (optional)
// radius breaks the query if using tax_query too
'radius' => 25 // Radius to select for in miles (optional)
));
And here's the normal WP_Query:
// add query for title
function title_filter( $where, &$wp_query ) {
global $wpdb;
if ( $search_term = $wp_query->get( 'title_like' ) ) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $search_term ) ) . '%\'';
}
return $where;
}
add_filter( 'posts_where', 'title_filter', 10, 2 );
// dates to and from
function date_from( $from ) {
$from = str_replace("meta_key = 'date_%_start-date'", "meta_key LIKE 'date_%_start-date'", $from);
return $from;
}
add_filter('posts_where', 'date_from');
function date_to( $to ) {
$to = str_replace("mt1.meta_key = 'date_%_end-date'", "mt1.meta_key LIKE 'date_%_end-date'", $to);
return $to;
}
add_filter('posts_where', 'date_to');
// convert date to yyyymmdd
$date1 = str_replace('/', '-', $_POST['when']);
$when = date("Ymd", strtotime($date1));
$date2 = str_replace('/', '-', $_POST['when-2']);
$when2 = date("Ymd", strtotime($date2));
?>
<h1>Search</h1>
<div class="events">
<?php $args = array(
// general
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
// what input
'title_like' => $_POST['what'],
// category filter
'tax_query' => array(
array(
'taxonomy' => 'main-cat',
'field' => 'slug',
'terms' => $_POST['main-cat']
)
),
// date filter
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'date_%_start-date',
'value' => $when,
'compare' => '>=',
'type' => 'DATE'
),
array (
'key' => 'date_%_end-date',
'value' => $when2,
'compare' => '<=',
'type' => 'DATE'
)
),
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $args );
$wp_query->query('posts_per_page=2&post_type=event'.'&paged='.$paged);
When used individually they work fine. But I'm not sure how I can run one of them, then use the result to put through the other query. Is this even possible?

Show latest post from each author if post is not older than a month

I have a list of the authors (Wordpress) on the site that appears on every page so the list exists outside of the loop.
I managed to show every authors' image with their names but I would like to get their latest post title that links to the post. The post title should only show when the post is not older than a month.
Any help would be appreciated.
Thanks
<?php
global $wpdb;
$query = "SELECT ID, user_nicename from $wpdb->users WHERE ID != '1' ORDER BY 'ASC' LIMIT 20";
$author_ids = $wpdb->get_results($query);
foreach($author_ids as $author) :
// Get user data
$curauth = get_userdata($author->ID);
// Get link to author page
$user_link = get_author_posts_url($curauth->ID);
$post_link = get_permalink($curauth->ID);
// Set default avatar (values = default, wavatar, identicon, monsterid)
$main_profile = get_the_author_meta('mainProfile', $curauth->ID);
$hover_profile = get_the_author_meta('hoverProfile', $curauth->ID);
$award_profile = get_the_author_meta('awardProfile', $curauth->ID);
?>
You can use WP_Query to create a new loop for you instead. It accepts a cool date_query argument since version 3.7. Untested, but should work.
EDITED:
$args = array(
'showposts' => 1,
'orderby' => 'date',
'date_query' => array(
array(
'after' => array(
'year' => date( "Y" ),
'month' => date( "m", strtotime( "-1 Months" ) ),
'day' => date( "t", strtotime( "-1 Months" ) ),
),
'inclusive' => true,
)
) );
$query = new WP_Query( $args );
Then you can just run a regular loop
// run the loop with $query
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo 'Latest post: ' . get_the_title();
}
} else {
// no posts
}

Wordpress custom loop displays posts based on the day...is posssible?

I would create a custom loop in the home page that will display only the posts that contains a specific word in its custom field.I'l try to explain it with an example.
EX. I got a website of a Basketball team and 2 custom post types called "COACh" and "TRAINING".
In "COACH" i add the coach and its skills.
In "TRAINING" i add the exrcise's name,the description and in the custom meta box i add the time,the coach,the duration and the day of the execution.
Now,suppose that it's Monday i would display all the TRAININGS that contains the day Monday in its custom field.
Is it possible without a plugin like event calendar????
IS IT RIGHT?
$today = date("l");
$args = array( 'post_type' => 'palinsesto', 'posts_per_page' => -1);
$palinsesto = get_posts( $args );
foreach ( $training as $post ) {
$day = get_post_meta( $post->ID, 'giorno ' ); // here day is key
if($day==$today)
while ($post->have_posts()) : $post->the_post(); ?>
Yes it is possible, I coded below roughly:
$args = array( 'post_type' => 'TRAINING', 'posts_per_page' => -1);
$training = get_posts( $args );
foreach ( $training as $post ) {
$day = get_post_meta( $post->ID, 'day ' ); // here day is key
if($day=='Monday')
echo $post->post_title.'<br />';
echo $post->post_content.'<br />';
}
this one is the solution for my question...it works great
$today = strftime('%A');
$day = get_post_meta( $post->ID, 'giorno ' );
$my_query = new WP_Query(array(
'post_type' => 'palinsesto',
'meta_query' => array(
array(
'key' => 'giorno',
'meta-value' => $value,
'value' => $today,
'compare' => '=',
))));
if (have_posts()) :
while ($my_query->have_posts()) : $my_query->the_post(); ?>

In Wordpress, can I use WP_Query with timestamp?

I have code like this,
$date_arg = array(
'after' => array(
'year' => $num_days_ago['year'],
'month' => $num_days_ago['mon'],
'day' => $num_days_ago['mday'],
),
'inclusive' => false,
);
$query = new WP_Query( array ( 'date_query' => $date_arg, 'post_type' => $post_type ...
This is working fine but I have to specify a exact date. Is it possible to use "timestamp" to make the query.
For example, I want to query posts that is within 24 hours. I can get the timestamp by
$ts = time() - DAY_IN_SECONDS
It is possible to get the posts that is created after $ts?
You could try constructing a date using the date() and strtotime() functions.
$date_query = array(
'after' => date('Y-m-d H:i:s', strtotime('-24 hours'))
);
I'd also recommend taking a look at this handy website - http://www.viper007bond.com/tag/wp_date_query/
IMHO better like so :
function filter_where($where = '') {
$where .= " AND post_date > '" . date('Y-m-d H:i:s', strtotime('-24 hours')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
and more general :
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last x days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";// here 30
return $where;
}
add_filter( 'posts_where', 'filter_where' ); // add the filter before setting object or before loop
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' ); // .. and do not forget to remove it
Or you could try something like this
SELECT post_title, IF (post_date_gmt < (DATE_SUB(CURDATE(), INTERVAL 24 HOUR)), 'false', 'true') AS A FROM wp_posts
WHERE post_type = "post" AND post_status = "publish"
http://www.w3schools.com/sql/func_date_sub.asp

Categories