WordPress - How to minimize my WordPress Query? - wordpress

I have a Wordpress query that I am running. It queries through days of the week, and at the moment, the only way I know to repeat the query for each day, is to run the query 7 times, manually changing the day of the week.
Is there a way to only run the query once?
My code:
<?php
$args=array(
'taxonomy' => 'day',
'term' => 'monday',
'post_type' => 'schedule',
'meta_key' => 'tr_show_time',
'orderby' => 'tr_show_time',
'order' => 'asc',
'posts_per_page' => 24,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
My code to execute for the schedule.
<?php endwhile; } wp_reset_query(); ?>
So at the moment, I have to change the "term" in each query, and have the same query 7 times. There must be a better way.
If there is a better way, can someone answer and teach me the answer?
Thanks
EDIT: I just want add that each "day" on the schedule has 24 items, one for each hour of the day, and they are displayed in order from 00:00 until 24:00 (00:00, 01:00, 02:00, etc) so this "ordering" needs to be maintained. The only way I could see to do this was to query each day seperatly, hence why I had 7 queries.

I solved it myself:
<?php
date_default_timezone_set('America/New_York');
$d=date("D");
$current_day = date ('l');
?>
<ul>
<?php
$args=array(
'taxonomy' => 'day',
'term' => $current_day,
'post_type' => 'schedule',
'meta_key' => 'tr_show_time',
'orderby' => 'tr_show_time',
'order' => 'asc',
'posts_per_page' => 24,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $show_name = get_post_meta( get_the_ID(), 'tr_show_name', true ); ?>
<?php $show_time = get_post_meta( get_the_ID(), 'tr_show_time', true ); ?>
my code here.
<?php endwhile; } wp_reset_query(); ?>
</ul>

Try using the tax_query parameter
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'day',
'field' => 'slug',
'terms' => array( 'monday', 'tuesday', 'wednesday' )
)
),
'post_type' => 'schedule',
'meta_key' => 'tr_show_time',
'orderby' => 'tr_show_time',
'order' => 'asc',
'posts_per_page' => 24,
'caller_get_posts'=> 1
);
For more information read up on taxonomy queries

Related

Only display upcoming events in Wordpress Widget

Through a shortcode I placed the following code which is intented to display a random upcoming event in the sidebar. At the moment it displays a random of ALL events. How would I change the code to only display events that are younger than the current date?
<?php
// Build a custom query to get posts from future dates.
$querystr = `
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'f21_event_startdate'
AND STR_TO_DATE(wpostmeta.meta_value,'j. M Y') >= CURDATE()
AND wposts.post_status = 'publish'
AND wposts.post_type = 'events'
ORDER BY STR_TO_DATE(wpostmeta.meta_value,'j. M Y') RAND
LIMIT 1
`;
$events = $wpdb->get_results($querystr, OBJECT);
if ($events):
global $post;
foreach ($events as $post):
setup_postdata($post); ?>
<?php the_field('f21_event_startdate'); ?>
<h2><a style="color:#1e73be;" href="<?php the_permalink() ?>"><?php the_title(); ?></h2>
<div style="margin-top:10px; margin-bottom: 10px;"><?php the_post_thumbnail('full'); ?></div></a>
<b><?php the_field('f21_event_sub_header'); ?></b>
<?php endforeach;
else : ?>
<li>Sorry, no events coming up.</li>
<?php endif; ?>
you can WP_Query(). check the code below.
$args = array(
'post_type' => 'events',
'posts_per_page' => -1,
'meta_key' => 'f21_event_startdate',
'meta_value' => date('Ymd'),
'meta_compare' => '>=',
'orderby' => 'rand',
'order' => 'ASC'
);
$upcoming_events = new WP_Query( $args );
Avoid the SQL query in WordPress for getting post-type data.
we have a function for that please use
$args = array(
'post_type' => 'tribe_events',
'post_status' => 'publish',
'posts_per_page' => 10,
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => '_EventStartDate',
'meta_type' => 'DATETIME',
'paged'=>$page,
'meta_query'=>array(
array(
'key' => '_EventStartDate',
'value' => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ),
'compare' => '>'
),),
'tax_query'=>array(array(
'taxonomy' => 'tribe_events_cat',
'field' => 'id',
'terms' =>$tribe_events_cat,
))
);
$events = new WP_Query($args);
Summary: if you are using custom fields in WordPress make sure your custom fields are stored as a post meta then you can easily filter that,
In the above code, we have a meta query where we can use key, value, and compare. thanks.

Prioritizing wp_query by meta key

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();
?>

How to display all posts in WordPress?

I want to display all post in my WordPress home page.
I have written following query for getting all post but I do not get all posts. It just displays 10 or 11 posts:
$args = array(
'post_type' => 'post',
'posts_per_page' => $number,
'order' => $sort_by,
'orderby' => 'title',
'post_status' => 'publish',
'tag' => $tags,
'ignore_sticky_posts' => 1,
);
$args['tax_query'] = array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => 'post-format-video',
));
$query = new WP_Query($args);
So please let me know how can I get all posts.
Displaying all posts that has been published. You have to use post_per_page='-1' to retrive all the posts.
$args = array(
'post_type'=> 'post',
'orderby' => 'ID',
'post_status' => 'publish',
'order' => 'DESC',
'posts_per_page' => -1 // this will retrive all the post that is published
);
$result = new WP_Query( $args );
if ( $result-> have_posts() ) : ?>
<?php while ( $result->have_posts() ) : $result->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
Hope so this will retrive all the posts as per your expectation.
try Multiple loops :
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<!-- do stuff ... -->
<?php endwhile; ?>
<?php endif; ?>
if you need Advanceded query use this :
<?php query_posts( 'post_type=post&posts_per_page=10'); ?>
and you can use query_posts() get all posts
Set the variable posts_per_page => -1
As so:
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'order' => $sort_by,
'orderby' => 'title',
'post_status' => 'publish',
'tag' => $tags,
'ignore_sticky_posts' => 1,
);
This will make Query get all posts in your table.
See more in the WP Query docs

Exclude duplicated posts from wordpress custom queries

I'm building a template for a homepage which shows 4 latest posts on top and some groups of posts divided by category around the page (I'm using get_posts to perform queries).
What I'd like to do is exclude from these category posts any post already present in the latest four news on top.
I guess I should get that four post IDs and use the 'post__not_in' parameter in the "category" queries, but I can't make it work.
Dou you have any hints?
This is the code:
// First query: I get last four posts
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => 4
);
// In my dreams, this should be the array of post IDs
$ids->query_vars['page_id'];
// Second query: I get 5 posts for a given categoory and exclude posts with IDs from first query
$query = new WP_Query($args);
$args2 = array(
'numberposts' => 5,
'category' => 15,
'orderby' => 'post_date',
'order' => 'DESC',
'post__not_in' => $ids
);
$query2 = new WP_Query($args2);
$primaposizione = get_posts( $args2 );
foreach ( $primaposizione as $post ) : setup_postdata( $post );
... do the stuff ...
endforeach;
wp_reset_postdata();
UPDATE
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => 4
);
$query = new WP_Query($args);
$excludeID = array();
while ( $query->have_posts() ) : $query->the_post();
$excludeID = $post->ID;
endwhile;
$args = array(
'posts_per_page' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'category' => 15,
'post_type' => 'post',
'post_status' => 'publish',
'post__not_in' => array($excludeID)
);
$primaposizione = get_posts( $args );
foreach ( $primaposizione as $post ) : setup_postdata( $post );
$category = get_the_category();
$slug = $category[0]->category_nicename ;
$esteso = $category[0]->cat_name;
if(has_post_thumbnail()) { ?>
<span class="hidden-xs"><?php the_post_thumbnail('bones-thumb-300', array('class' => 'img-responsive')) ?></span>
<?php } ?>
<h3 class="ellipsis"><?php the_title(); ?></h3>
<?php the_excerpt();?>
<?php endforeach;
wp_reset_postdata();
?>
The way you're trying to select the ID's is not going to work. You have to call wp_query first. I think this should work:
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => 4
);
$query = new WP_Query($args);
$excludeID = array();
while ( $query->have_posts() ) : $query->the_post(); ?>
$excludeID[] = $post->ID; // forgot the brackets
// do stuff
endwhile;
(...)

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