I need to display "Featured posts" randomly for each day of the week on a single page. Posts need to be current.
What I want to do:
I load 7 posts in a variable using WP_Query, test each post to see if it's current, and display only the remaining ones.
Problem: I can't find how to delete an entry in a while loop.
Init of the page:
$args = array(
'post_type' => 'listing',
'post_status' => 'publish',
'post__not_in' => array($post->ID),
'orderby' => 'date',
'order' => 'DESC',
'meta_key' => 'featured_calendar',
'meta_value' => 'on',
'posts_per_page' => 7
);
$featured = new WP_Query($args);
if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post();
$theending = get_post_meta($post->ID, 'theend', true);
if ($theending > time()){
echo "All Good";
} else{
//Delete the entry
}
endwhile;
endif;
Inside the days of the week loop:
if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post();
//Display of remaining posts
endwhile;
endif;
It sounds like you should just modify the query to exclude those posts in the first place:
$args = array(
'post_type' => 'listing',
'post_status' => 'publish',
'post__not_in' => array($post->ID),
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'featured_calendar',
'value' => 'on',
'compare' => '='
),
array(
'key' => 'theend',
'value' => time(),
'compare' => '>',
'type' => 'NUMERIC' // Not sure if you want a TIME here
),
),
'posts_per_page' => 7
);
$featured = new WP_Query($args);
Read more about custom field parameters in the Codex.
If you're looking to remove a row from an array, you can do that like this:
unset ($array[$key]);
$recent_featured = array();
$featured = new WP_Query($args);
if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post();
$theending = get_post_meta($post->ID, 'theend', true);
if ($theending > time()){
$recent_featured[] = $post;
} else{
}
endwhile;
now you can use $recent_featured, pass it, loop over it, display them...
Related
I'm trying to create a query in WordPress that displays only the posts that were edited today, excluding those posted today. I've tried several variations but nothings seem to be working:
$today = current_time('Ymd');
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => '10',
'meta_query' => array(
array(
'key' => 'modified',
'compare' => '>=',
'value' => $today,
'type' => 'NUMERIC,'
)
),
'orderby' => 'modified',
'order' => 'DESC',
'ignore_sticky_posts' => '1'
);
I'm not quite sure what to put in key, although that isn't the only problem.
If I get it right, with "displays only the posts that were edited today, excluding those posted today."
I guess you mean display ONLY old published posts modified/edited today.
If that is the case, this might help you:
<?php
// query args
$args = array(
'posts_per_page' => '10',
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'modified',
'order' => 'DESC',
'ignore_sticky_posts' => '1',
'caller_get_posts' => 1
);
// query
$updated = new WP_Query($args);
// loop
while($updated->have_posts()) : $updated->the_post();
$today = current_time('Y-m-d'); // current date a.k.a. TODAY
$pub = get_the_time('Y-m-d', $updated->ID); // date when post was published
$mod = get_the_modified_time('Y-m-d', $updated->ID); // date when post was last modified
// if post NOT published today AND was modified today display:
if ( $pub !== $today && $mod === $today ) :
?>
<!-- here goes your normal wp game -->
<h1><?php the_title ?></h1>
<span><?php the_date(); ?></span>
<p><?php the_excerpt(); ?></p>
<?php endif; endwhile; ?>
Its not the best solution but you can just do the filter after your query and check if the current date string is inside the post date modified,
e.g.
$ar = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => '10',
'orderby' => 'modified',
'order' => 'DESC',
'ignore_sticky_posts' => '1'
);
$q = new WP_QUery( $ar );
$p = $q->get_posts();
foreach( $p as $a ) {
$c = current_time( 'Y-m-d' );
if ( strpos( $a->post_modified, $c ) !== false ) {
_e( $a->post_title .' '.$a->post_modified. ' - ' . $c. "<br>" );
}
}
#echo '<pre>', print_r($p, 1), '</pre>';
Based on this query to select all posts either published or modified today, you could just write this WP_Query to retrieve only the modified ones:
$args = array(
'post_type' => 'post',
'post_status' => 'any', // we also want the drafts
'nopaging'=>true,
'date_query' => array(
'column' => 'post_modified',
'year' => $day_parsed['year'],
'month' => $day_parsed['month'],
'day' => $day_parsed['day'],
)
);
$query_day_posts = new WP_Query( $args );
Question for all of ya. I have the current query running on my search page template, and it seems to be working fine if my search query seems to be included in the title of the post, but when I include a meta query to try and also look in another spot for the search term, it doesn't gather any results, only the same results it had before.
Second question, for some reason it still is only displaying 6 (the number of posts set in WP Admin) posts, and not listening to the query.
<?php // WP_User_Query arguments
$search_term = get_search_query();
$args = array (
'post_type' => 'courses',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'nopaging' => true,
's' => '*'.$search_term.'*',
'meta_query' => array(
array(
'key' => 'course_id',
'value' => $search_term,
'compare' => 'LIKE'
)
)
);
$wp_course_query = new WP_Query($args);
// Get the results
$courses = $wp_course_query; ?>
<?php // Check for results
if (!empty($courses->get_posts())) { ?>
<ul class="course-list">
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<li> <?php the_title(); ?> </li>
<?php endwhile; endif; wp_reset_query(); ?>
</ul>
<?php } else { ?>
<p>No courses match that query</p>
<?php } ?>
Things I've tried:
Hard coding the value, nothing there.
Removing * from 's'
It seems that this is just impossible in WordPress, so I had to do this another way.
$search_term = get_search_query();
$args = array (
'post_type' => 'courses',
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'nopaging' => true,
's' => $search_term
);
$args2 = array (
'post_type' => 'courses',
'posts_per_page' => -1,
'nopaging' => true,
'meta_query' => array(
array(
'key' => 'course_id',
'value' => $search_term,
'compare' => 'LIKE'
)
)
);
$courses1 = get_posts($args);
$courses2 = get_posts($args2);
$merged = array_merge($courses1, $courses2);
$post_ids = array();
foreach ($merged as $item) {
$post_ids[] = $item->ID;
}
$unique = array_unique($post_ids);
$posts = get_posts(array(
'post_type' => 'courses',
'order' => 'ASC',
'orderby' => 'title',
'post__in' => $unique,
'posts_per_page' => -1
)); ?>
I'm trying to use the meta_query in WooCommerce product page.
This is the code I'm using:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' =>8,
'meta_query' => array(
array(
'key' => 'autor',
'value' => '"'.get_the_ID().'"',
'compare' => 'LIKE',
)
),
);
$products = new WP_Query($args);
if ($products->have_posts()) :
$i=0;
while ($products->have_posts()) : $products->the_post();
$autor = get_field('autor');
if($i % 2 ==0) ?>
<h3><?php the_title();?></h3>
<?php if ($i % 2 != 0)
$i++;
endwhile;endif;?>
It doesn't show any title, if I remove the meta_query it shows all products so the problem is that the relation meta_query code is not working. Any ideas how to use it on WooCommerce template?
You use get_the_ID() to get the author id in meta_query args.
get_the_ID() - will get the Post id, not the author id.
To get all posts by authoк id your args should look like this:
$args = array(
'author' => 1,
'post_type' => 'product',
'posts_per_page' => 8,
);
I also see that you using get_field()-function. WordPress core does not have this function. You can use instead get_the_author().
Eventually your code will look like:
<?php
$args = array(
'author' => 1,
'post_type' => 'product',
'posts_per_page' => 8,
);
$products = new WP_Query($args);
if ($products->have_posts()) :
$i=0;
while ($products->have_posts()) : $products->the_post();
$autor = get_the_author();
if($i % 2 ==0) ?>
<h3><?php the_title();?></h3>
<?php if ($i % 2 != 0)
$i++;
endwhile;endif;?>
You can use this code snippet for meta_query in woocommerce
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page'=> 10,
'orderby' => 'total_sales',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_featured',
'value' => 'yes',
'compare' => '='
),
array(
'key' => 'total_sales',
'value' => '10',
'compare' => '>='
)
)
);
$query = new WP_Query( $args );
Using get_posts(), I need to first retrieve posts that fall on a certain day (the day is set by a custom field - just the date, not time). I do this by using a meta key/value. Then, I need to order these posts based on the time of day (which is a separate custom field, just time, not date). So essentially I need to pull in all the events that fall on a given day, and order them according to the time.
First I grab the day, using a custom field:
if ( get_field('festival_day') ) {
$day_stamp = get_field('festival_day');
}
Then I set my arguments for the query:
$args = array(
'posts_per_page' => -1,
'post_type' => 'event',
'meta_key' => 'event_date',
'meta_value' => $day_stamp
);
$events = get_posts( $args );
So.. the question is, how do I query the other custom field (which is the start time), and then sort by that time? The time field key is event_start_time.
Thanks!
You can user WP_Query to retrieve your events and you can query it something like below:
$args = array(
'post_type' => 'event',
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_key' => 'event_start_time',
'meta_query' => array(
array(
'key' => 'event_start_time',
'value' => 'yourValue_here',
'compare' => '>='
),
array(
'key' => 'event_date',
'value' => $day_stamp,
'compare' => '='
)
)
);
$query = new WP_Query( $args );
UNTESTED but it should work.
Check this code, it should work to sort your post according to time from resulting post of day.
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_key' => 'event_start_time',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query'=> array(
array(
'key' => 'event_date',
'value' => $day_stamp,
'compare' => 'IN'
)
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>'. get_the_title().'</li>';
}
echo '</ul>';
} else {
// no posts found
}
The following argument works properly for search results, however they aren't displaying in the proper ascending order. I'm looking to display the most expensive at the top, and so on.
Here's my code, where the $input_price is user defined by preset values within a form.
$args = array(
'post_type' => 'post',
's' => $query,
'post_status' => 'publish',
'cat' => "$category_name",
'meta_query' => array(
array(
'key' => 'price',
'value' => "$input_price",
'type' => 'numeric',
'compare' => '<=',
'order' => 'ASC'
)
)
);
$the_query = new WP_Query( $args );
?>
I don't think this is the problem, but the 'price' is being output with currency and comma values, as seen below.
<?php $meta = get_post_custom($post->ID);
echo '$'.number_format($meta['price'][0], 0, '.', ',').''; ?>
Annnd here's the loop to display
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h1><?php the_title() ;?></h1>
<?php $meta = get_post_custom($post->ID);
echo '$'.number_format($meta['price'][0], 0, '.', ',').''; ?>
<?php the_excerpt(); ?>
<?php endwhile; else: ?>
<p>Sorry, there are no products within those search terms! Try Adjusting the maximum price.</p>
Thanks!
try DESC instead of ASC for greatest to smallest?
Found the answer!
The code below works as intended. I re-ordered my array slightly to include 'orderby' at the very end, while also specifying which key to order by.
<?php
$args = array(
'post_type' => 'post',
's' => $query,
'post_status' => 'publish',
'cat' => "$category_name",
'meta_query' => array(
array(
'key' => 'price',
'value' => "$input_price",
'type' => 'numeric',
'orderby' => 'meta_value_num',
'compare' => '<=',
)
),
"meta_key"=>"price",
"orderby"=>"meta_value_num",
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>