Prioritizing wp_query by meta key - wordpress

I have two custom fields for views. weekly_views and all_views. The weekly views custom field is deleted every week and starts counting views again from 0. So now what I want to achieve is show 12 posts by weekly views but when the custom field is deleted and unless there are views on those posts the query shows nothing. I want to show here posts by all_views instead of no posts.
My query goes as follows but it's not working as I want. In short what I want to achieve is to show posts by weekly_views custom field but if there's no post then show posts by all_views. And also if there's less than 12 posts by weekly_views then show weekly_views posts first and then remaining posts by all_views.
$args = array(
'post_type' => array( 'custom_post_type_1', 'custom_post_type_2'),
'posts_per_page' => '12',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'weekly_views',
),
array(
'key' => 'all_views',
),
),
);
The above code is returning me posts but are sorted by all_views.
Edit
The new query that's working for me
<?php
$args = array(
'post_type'=> array( 'custom_post_type1', 'custom_post_type2'),
'posts_per_page' => '12',
'meta_key' => 'weekly_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$the_query = new WP_Query( $args );
if ($the_query->post_count < 12) {
$countweeklyposts = $the_query->post_count;
$showallpostscount = 12 - $countweeklyposts;
$args2 = array(
'post_type'=> array( 'band', 'artist'),
'posts_per_page' => $showallpostscount,
'meta_key' => 'all_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$the_query2 = new WP_Query( $args2 );
}
?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
//Code to show posts goes here
<?php
endwhile;
wp_reset_postdata();
?>
<?php while ($the_query2 -> have_posts()) : $the_query2 -> the_post(); ?>
//Code to show posts goes here
<?php
endwhile;
wp_reset_postdata();
?>

You could do this too if you want a little less code
<?php
$args = array(
'post_type'=> array( 'custom_post_type1', 'custom_post_type2'),
'posts_per_page' => '12',
'meta_key' => 'weekly_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$args2 = array(
'post_type'=> array( 'band', 'artist'),
'posts_per_page' => '12',
'meta_key' => 'all_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
if ($query->post_count > 12) {
$query_args = $args;
}else if($query->post_count < 12){
$query_args = $args2;
}
$query = new WP_Query( $query_args );
while ($query -> have_posts()) : $query -> the_post();
//Code to show posts goes here
endwhile;
wp_reset_postdata();
?>

Related

Wordpress — display events (custom post type) for the current week (from monday to sunday included)

I'm working on a wordpress of a soccer club and I want to display events (matches) for the current week.
I have to sort by ACF field called "date_match" and not the date of the post itself but it doesn't work.
Matches are custom post type.
Here is my query
<?php
//define args
$args = array(
'post_type' => 'matchs',
'orderby' => 'meta_value',
'meta_key' => 'date_match',
'order' => 'ASC',
'posts_per_page' => 4,
// Using the date_query to filter posts from last week
'meta_query' => array(
array(
'key' => 'date_match',
'year' => date( 'Y' ),
'week' => date( 'W' )
)
)
);
//query
$the_query = new WP_Query( $args );
//loop
if ($the_query->have_posts()): while ($the_query->have_posts()) : $the_query->the_post();
?>
…
<?php endwhile; ?>
<?php else: ?>
<!-- article -->
<article>
<h6>No match to display.</h6>
</article>
<!-- /article -->
<?php endif; ?>
And here is my configuration in ACF :
You can try something like:
$start = 'define your start date';
$end = 'define your end date';
// update your meta_query to search for posts between start and end
'meta_query' => array(
array(
'key' => 'date_match',
'value' => array($start, $end),
'compare' => 'BETWEEN',
'type' => 'DATE'
)
<?php
//define args
//timestamp used to format the date
$thisMonday = strtotime('this week');
$thisFriday = strtotime('+6 days', $thisMonday);
$thisMonday = date('Ymd', $thisMonday);
$thisFriday = date('Ymd', $thisFriday);
$args = array(
'post_type' => 'matchs',
'orderby' => 'meta_value',
'meta_key' => 'date_match',
'order' => 'ASC',
'posts_per_page' => 4,
// Using the date_query to filter posts from this week
'meta_query' => array(
array(
'key' => 'date_match',
'value' => $thisMonday,
'compare' => '>='
),
array(
'key' => 'date_match',
'value' => $thisFriday,
'compare' => '<='
)
)
);
//query
$the_query = new WP_Query( $args );
//loop
if ($the_query->have_posts()): while ($the_query->have_posts()) : $the_query->the_post();
?>

Wordpress wp_query error in marketpress

I am using the following function to get markerpress products in the category 'featured' and display the product images in a slideshow.
$args = array(
'category_name' => 'featured',
'post_type' => 'product'
);
$wp_query1 = new WP_Query( $args );
if (have_posts()) : while ( $wp_query1->have_posts() ) : $wp_query1->the_post()
Problem is that it does not fetch any data. I am forced to just use the below function which displays all the data.
$wp_query1 = new WP_Query('post_type=product');
What am I doing wrong and how can I set limits and also sort the data. Thanks.
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category_name' => 'featured',
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'product',
'post_status' => 'publish'
);
$posts_array = get_posts( $args ); ?>

How do I show most viewed posts on a per-month basis?

For last month this query worked, now this 3 days (new month started), no posts at all:
<?php $current_year = date('Y'); ?>
<?php $current_month = date('m'); ?>
<?php endif;
arras_featured_loop( arras_get_option('featured1_display'),
apply_filters('arras_featured1_query', array(
'list' => $featured1_cat,
'taxonomy' => arras_get_option('featured1_tax'),
'query' => array( 'posts_per_page' => 15,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'year' => $current_year,
'monthnum' => $current_month,
'category_name' => Music,
'posts_per_page' => $featured1_count,
'exclude' => $post_blacklist,
'post_type' => arras_get_option('featured1_posttype')
)
) ) );
?>
Any kind of help will be appreciated.
UPDATE:
This was not pissible.
But now I found way to do this with plugin Wordpres Popular Posts.
Its working alone in template with this code:
<?php
if (function_exists( 'wpp_get_mostpopular' )) {
wpp_get_mostpopular('range=weekly&cat=276&order_by=views&limit=8');
}
?>
Now, what I need to do is to use this code up into this template down:
<?php endif;
arras_featured_loop( arras_get_option('featured1_display'), apply_filters('arras_featured1_query', array(
'list' => $featured1_cat,
'taxonomy' => arras_get_option('featured1_tax'),
'query' => array(
'posts_per_page' => $featured1_count,
'exclude' => $post_blacklist,
'post_type' => arras_get_option('featured1_posttype')
)
) ) );
?>
I think I need to put it inside here:
'query' => array(
Problem is I don't know to turn query from existing to one with arrows 'range=weekly' to 'range' => weekly didnt worked.

WordPress Loop - skip posts without a thumbnail

I want skip every post that has no thumbnail. The code does not work properly yet.
Actually the script doesn't show posts without a thumbnail - that's good, but in the loop the post with no thumbnail is still counted as a post.
So when i have for example 10 posts in my wordpress database. I want show 5 of them. But only the posts who has a thumbnail.
<ul>
<?php
$args = array( 'numberposts' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
$my_posts = get_posts( $args );
global $post;
foreach( $my_posts as $post ) : setup_postdata($post);
if ( !has_post_thumbnail() ) {
continue;
} else {
?>
<li>
<div class="clearfix" >
<div class="thumb"><?php the_post_thumbnail('post-image-big'); ?></div>
<?php the_title(); ?>
<p class="category"><?php the_category(', '); ?></p>
</div>
</li>
<?php } ?>
<?php endforeach; ?>
</ul>
Try
$args = array( 'numberposts' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish' ,
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => '!=',
'value' => ''
)
)
);
or this if checking for an empty string didn't work for you
$args = array( 'numberposts' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish' ,
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => '!=',
'value' => null
)
)
);

Wordpress custom field date, display post with custom date > current in ascending order

How do I order post by a custom field if "releasedate" > "currentdate" but in acending order. Basically only display post with dates starting after today but in ascending order. currently i have
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('showposts=10&post_type=movies&meta_key=releasedate_value&orderby=releasedate_value&order=ASC');
if (have_posts()) : while (have_posts()) : the_post();
$currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
?>
<?php if ($releasedate > $currentdate) {?>
"my contents/ post here"
<?php } ?>
<?php endwhile; ?>
<?php endif; ?>
now everything works except when its ASC and not DSC, no post will display because wordpress in getting the post first and the erasing the post that are not before current date and only 10 post are allowed, therefore if 10 post has a release date before today, wordpress loads then erase them after and leaves everything blank! Thank you for your help
I'd put the date criteria into the query itself. Assuming you have WordPress version 3.1 or higher, you can use the meta_query parameter. Something like:
<?php
$currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
$wp_query = new WP_Query( array ('showposts' => 10,
'post_type' => 'movies',
'meta_query'=> array(
array(
'key' => 'releasedate_value',
'compare' => '>',
'value' => $currentdate,
'type' => 'DATE',
)),
'meta_key' => 'releasedate_value',
'orderby' => 'meta_value',
'order' => 'ASC'
)
);
should work.
This works with me too:
$currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
$args = array(
'post_type' => 'event',
'meta_key' => 'date_field',
'posts_per_page' => 5,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'date_field',
'value' => $currentdate,
'compare' => '>=',
'type' => 'DATE'
),
),
);
$my_query = null;
$my_query = new WP_Query($args);

Resources