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!
Related
This is the custom query I'm running to display post from 2 categories.
I have installed WordPress Plugin "Featured Post" but the featured post is not excluding from the displayed list.
<?php
$category_id = get_cat_ID($strReports || $strInsights);
$custom_query = new WP_Query( 'cat=' .$category_id. '&featured=no&posts_per_page=6&order=desc' );
while($custom_query->have_posts()) : $custom_query->the_post();
?>
HTML Content Here
<?php endwhile; ?>
<?php wp_reset_query(); // reset the query ?>
Looking through "Featured Post" you can see that it just test against yes value. What feature=yes does is just check a meta field, so I think you can do the reverse way to achieve what you want, like this:
$args = array(
'cat' => $category_id,
'posts_per_page' => 6,
'order' => 'DESC', // since order default value is already DESC you can remove this line
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_is_featured',
'value' => 'yes',
'compare' => '!=',
),
array(
'key' => '_is_featured',
'value' => 'foo', // Prior to WP 3.9 we have to provide any non-empty string here
'compare' => 'NOT EXISTS',
),
) ,
);
$custom_query = new WP_Query($args);
Hope it helps!
your'e running an invalid argument on get_cat_ID.
should be something like this:
<?php
$cat_names = array('Cat_Name1', 'Cat_Name2');
$category_Name = implode(',', $cat_names);
$args = array(
'category_name' => $category_Name,
'posts_per_page' => 6,
'meta_query' => array(
array(
'key' => 'featured',
'value' => true,
'compare' => '!=',
),
),
);
$custom_query = new WP_Query( $args );
while($custom_query->have_posts()) : $custom_query->the_post();
?>
<?php the_title();?>
<?php endwhile; ?>
<?php wp_reset_query(); // reset the query ?>
create your custom query and add filter.
function SearchFilter($query){
$query=" SELECT Distinct SQL_CALC_FOUND_ROWS p.* FROM `$wpdb->posts` as p left join `$wpdb->postmeta` as m on m.post_id=p.ID "
." left join `$wpdb->term_relationships` as r ON (p.ID = r.object_id) "
." left join `$wpdb->term_taxonomy` as x ON (x.term_taxonomy_id = r.term_taxonomy_id) "
." left join `$wpdb->terms` as t ON (t.term_id = x.term_id AND x.taxonomy='category') "
." WHERE p.post_type = 'post' AND p.post_status = 'publish' "
." AND t.term_id='$idcategorie' "
return $query;
}
add_filter( 'posts_request', 'SearchFilter' );
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');
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
}
I've written this loop to pull in posts that get the most views within the last 30 days (logging the view count to the DB as a custom meta key).
Though, upon thinking about it a little more I'm not really sure if I'm going about it the 'right' way, or at least the most logical way.
<?php
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$popularpost = new WP_Query( array(
'posts_per_page' => 33,
'meta_key' => 'wpb_post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC'
) );
remove_filter( 'posts_where', 'filter_where' ); ?>
<?php if ( $popularpost->have_posts() ) : ?>
<?php while ( $popularpost->have_posts() ) : $popularpost->the_post(); ?>
<article <?php post_class('item-post block'); ?> id="post-<?php the_ID(); ?>">
<?php the_title (); ?>
</article> <!-- end div post -->
<?php endwhile; wp_reset_query(); ?>
<?php endif; ?>
So, as you can see I'm limiting the date range to 30 days, ordering the posts by the meta_value_num and getting 33 posts.
So, logically what's happening is any post published within the last 30 days will be displayed here in order of how many views they've had.
What got me thinking about it is when I created a page to pull the 33 most viewed posts from the last 7 days, they're the same. Obviously.
So I think what I want to be doing is getting posts by how many views they've had IN the last 30 days/7 days, not posts published within those date ranges.
Am I thinking along the right lines? If so, can you give me any idea of how to go about that?
If you're running the WordPress 3.7 or later, you now can use date_query. Not tested, but your query will look something like:
$popularpost = new WP_Query( array(
'posts_per_page' => 33,
'meta_key' => 'wpb_post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'date_query' => array(
array(
'after' => '1 month ago',
'column' => 'post_date_gmt', //posted within one month
),
)
)
);
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
strtotime('30 days') = now + 30 days
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(); ?>