I'm using "The Future Is Now!" plugin and want to display all post that are schedule for the future. The only problem is, how do i make a query like (WHERE date >= $currentdate) before i enter the loop?
<?php if (is_category('My awesome category')) {
$currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
/* Some sort of query with the statement date >= $currentdate? */
}
?>
/* The Loop */
<?php if (have_posts()) : while (have_posts()) : the_post();
?>
query_posts(array('post_status' => 'future'));
Edit: Above is the easy answer that fits with your loop, but as a default solution it's better that u use a new $WP_Query object:
$my_query = new $WP_Query;
$my_query->query_posts(array('post_status' => 'future'));
while ($my_query->have_posts()) :
$my_query->the_post();
the_title();
endwhile;
wp_reset_postdata(); // now the main wordpress query and post data is intact
2nd Edit: Similar query but with a filter:
function filter_where( $where = '' ) {
// posts in the future
$now = date("Y-m-d H:i:s");
$where .= " AND post_date >= '$now'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$q = new WP_Query(array('post_type' => 'post'));
while ($q->have_posts()) :
$q->the_post();
the_title();
endwhile;
remove_filter( 'posts_where', 'filter_where' );
Related
I have a custom post called Project.
I need to automatically update a field with 100 when the post date is over 7 days.
I tried the following coed in function.php but it doesn’t auto update the field.
Would you please let me know how to fix the code?
function update_active_based_on_date() {
// query to get all custom posts - project
$args = array(
'post_type' => 'project',
'posts_per_page' => -1
);
$query = new WP_Query($args);
// if posts are returned and more than 7days, update infosubmit field
if ($query->have_posts()) {
global $post;
$timestamp = get_the_time( 'U', $post->ID );
$diff = current_time('timestamp') - $timestamp;
while ($query->have_posts() && $diff > 604800) {
$query->the_post();
$field_key = "field_60836f942ae12";
$value = "100";
update_field($field_key, $value, $post->ID);
} // end while have_posts
wp_reset_postdata();
} // end if have_posts
} // end function
Thank you.
You can achieve this like:
function update_active_based_on_date() {
// query to get all custom posts - project
$args = array(
'post_type' => 'project',
'posts_per_page' => -1
);
$query = new WP_Query($args);
if ($query->have_posts()) {
global $post;
while ($query->have_posts()) {
$query->the_post();
// if posts are returned and more than 7days, update infosubmit field
$diff = time() - strtotime(get_the_date());
if($diff > 604800){
$field_key = "field_60836f942ae12";
$value = "100";
update_field($field_key, $value, $post->ID);
}
} // end while have_posts
wp_reset_postdata();
} // end if have_posts
} // end function
add_action('init','update_active_based_on_date');
I am building featured posts section on my site using Wordpress tags (3 latest posts tagged as "featured" are being shown on the homepage) using this code:
<?php
$args=array(
'tag' => 'featured',
'showposts'=>3,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="featured-article">
<!--thumbnail, title and link code here -->
</div>
<?php
endwhile;
} //if ($my_query)
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Every entry is wrapped with "featured-article", but since i want the first post to be full-width and other 2 half width, i was wondering how can i add appropriate classes to them?
So, the first post gets "full-width" class and other two "half-width"...
I apologize if i didnt explained it properly (english not first language and all).
Any help would be greatly appreciated
You can get the index of your post with $my_query->current_post while you're in the loop.
$class = $my_query->current_post == 0 ? 'full-width' : 'half-width';
You might also want to make sure that the class only gets applied to the first item of the first page
$class = $my_query->current_post == 0 && !is_paged() ? 'full-width' : 'half-width';
Here's your loop
<?php
$args=array(
'tag' => 'featured',
'showposts'=>3,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$class = $my_query->current_post == 0 && !is_paged() ? 'full-width' : 'half-width'; ?>
<div class="featured-article <?php echo $class; ?>">
</div>
<?php
endwhile;
} //if ($my_query)
wp_reset_query(); // Restore global post data stomped by the_post().
?>
You can solve this with counter.
<?php
$args=array(
'tag' => 'featured',
'showposts'=>3,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
//add classes
$counter = 0;
$classes = '';
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$classes = $counter == 0 ? ' full-width' : ' half';
?>
<div class="featured-article<?php echo $classes; ?>">
<!--thumbnail, title and link code here -->
</div>
<?php
$counter++;
endwhile;
} //if ($my_query)
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Or You can make a custom filed for addition class.
I'm trying to make a little "latest news" section on my custom homepage in Wordpress, that outputs:
2 most recent news stories
Their titles
The excerpt
The link
I've tried taking the standard loop from the codex to see what I get first, but I get nothing. I am a bit confused, as I can't work out why it's not even outputting ANY posts, no content at all using just the basic loop:
<?php
// The Query
$the_query = new WP_Query( 'post_count=2' );
// The Loop
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
echo 'No news is good news!';
}
/* Restore original Post Data */
wp_reset_postdata();
?>
This code presently shows the "no news is good news" message. There are two published posts.
Your code does render output on my side, so it is working. You have one problem though, post_count is a property and not a parameter of WP_Query. You should be using posts_per_page
What I do believe is happening why you don't get any output, is that you are using custom post types, and not normal posts, which in this case will render no output as you don't have any normal posts.
Just change this line
$the_query = new WP_Query( 'post_count=2' );
to
$the_query = new WP_Query( 'posts_per_page=2&post_type=NAME_OF_POST_TYPE' );
You're passing the variable $args into WP_Query but not actually defining it.
Try this:
$args = array(
'post_type' => 'post',
'posts_per_page' => 2,
'no_found_rows' => false,
);
$the_query = new WP_Query( $args );
Then to output the content you need:
if ( $the_query->have_posts() ) :
echo '<ul>';
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
</li>
<?php endwhile;
echo '</ul>';
else :
// no posts found
echo 'No news is good news!';
endif;
wp_reset_postdata();
You don't need to use this alternative syntax for the if statement but it's common to see it written this way.
I noticed since writing this answer you updated your question passing in 'post_count=2' to WP_Query. You need to use 'posts_per_page' instead. post_count is a property of the query object and not a parameter.
This should only return the last two posts published.
<?php
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 2,
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() )
{
echo '<ul>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li>
<h1><?php the_title(); ?></h1>
<?php the_excerpt(); ?>
</li>
<?php endwhile;
echo '</ul>';
<?php
}
else
{
echo 'No news is good news!';
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
(The above is slightly changed from the post here)
I have set up a few categories and want to display specific ones based on is_page().
Inside page.php, I've created an if..else statement that checks the page name and prints out the specific category. My problem at the moment is that instead of just the_title being printed out the whole post is being printed.
Where am I going wrong with this?
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php if ( is_page( 'Greywater Recycling' ) ) { ?>
<div class="col">
<?php query_posts( 'category_name=Greywater Recycling&posts_per_page=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
</div>
<?php } else if ( is_page( 'Stormwater Management' ) ) { ?>
<div class="col">
<?php query_posts( 'category_name=Stormwater Management&posts_per_page=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
</div>
<?php } else if ( is_page( 'Rainwater Harvesting' ) ) { ?>
<div class="col">
<?php query_posts( 'category_name=Rainwater Harvesting&posts_per_page=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
</div>
<?php } ?>
<?php endwhile; // end of the loop. ?>
Many problems with your code. For one, is_page does not work inside the loop.
Second, don't mess with query_posts: When should you use WP_Query vs query_posts() vs get_posts()?. Really, forget about it for secondary loops.
Your code can be simplified to the following. In functions.php, we drop one function to get the Category ID by its Name. And another to do a secondary loop using the ID. And then, in page.php a simple call to those functions.
Documentation: Class_Reference/WP_Query.
page.php
Notice that you don't need to open PHP tags at each line, that makes the code dreadly difficult to read.
Use it only to swap between PHP and HTML
<?php
get_template_part( 'content', 'page' );
// This page title
$the_title = get_the_title();
// Search for category with the same name of the page
$category = brsfl_get_category_id( $the_title );
// Category found, go to the secondary loop
if( $category ) {
brsfl_check_print_categories( $category );
}
functions.php
Always prefix your function names with something distinctive to avoid conflicts that may take the site down.
/**
* Get the category ID based on its name
*/
function brsfl_get_category_id( $cat_name )
{
$term = get_term_by( 'name', $cat_name, 'category' );
// term found, return its ID
if( $term ) {
return $term->term_id;
}
// not found
return false;
}
/**
* Print a loop based on a category ID
*/
function brsfl_check_print_categories( $cat_id )
{
$args = array(
'post_type' => 'post',
'cat' => $cat_id,
'posts_per_page' => 5
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() )
{
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<h2>' . get_the_title() . '</h2>';
endwhile;
}
else
{
echo 'No posts found...';
}
}
Although I've answered within the proposed scope, I think there are better solutions to this, like:
a shortcode, just adapt the functions.
using Advanced Custom Fields to show a meta box where you can select a very specific category (don't relying in page and category names) and use only the WP_Query function to print it out in the template.
i think it's better to get the category by the slug name.
i've tried your solution, it works fine but in the case of one of my category has a name with special html characters like apostrophes or quotes it doesn't.
here is the piece of code edited from your solution :
in your functions.php
function brsfl_get_category_id($cat_name){
$term = get_term_by( 'slug', $cat_name, 'category' );
if( $term ) {
return $term->term_id;
}
return false;
}
in your page.php
$the_slug = get_post(get_the_ID())->post_name;
$category = brsfl_get_category_id( $the_slug );
if( $category ) {
brsfl_check_print_categories( $category );
}
I had created a custom search page containing the code
<?php
function filter_where( $where = '' ) {
$where .= " AND post_date >= '{$_GET['frmdate']}' AND post_date < '{$_GET['todate']}'";
return $where;
}
add_filter( 'posts_where', 'filter_where');
$query = new WP_Query( array( 'post_type' => 'post' ) );
while( $query->have_posts() ) : $query->the_post(); ?>
<p><?php the_title(); the_date() ?></p>
<?php endwhile; ?>
<?php remove_filter( 'posts_where', 'filter_where' ); wp_reset_query(); ?>
My Aim is Search posts with 'keywords' between 'Two Dates'
my search variables passing through url like this
www.something.com?page_id=372&s12=lorem&s13=ipsum&frmdate=2012-03-01&todate=2012-06-12
with my Current code to filter the result between two date is Working perfectly when my url is looks like this www.something.com?page_id=372&frmdate=2012-03-01&todate=2012-06-12
when iam adding &s=lorem isn't work both keyword and date filtering. even I add it (&s=lorem) alone
Rather than use a custom page, you may want to just alter the search template that's available in the WP template hierarchy. Please see this post for more information.
Updated My code with the below one its working fine with my all requirements, Not sure if there is an another smarter way
<?php
function filter_where( $where = '' ) {
if(isset($_GET['frmdate']) && isset($_GET['todate'])){
$where .= " AND post_date >= '{$_GET['frmdate']}' AND post_date < '{$_GET['todate']}'";
}
if(isset($_GET['s12'])){
$where .= " AND post_content LIKE '%{$_GET['s12']}%' " ;
}
if(isset($_GET['s13'])){
$where .= " AND post_title LIKE '%{$_GET['s13']}%' " ;
}
//echo $where;exit;
return $where;
}
add_filter( 'posts_where', 'filter_where');
$query = new WP_Query( array( 'post_type' => 'post' ) );
while( $query->have_posts() ) : $query->the_post(); ?>
<li>
<h4><?php the_title(); ?></h4>
<div class="monthDate"> <span class="date"><?php the_time('M d,Y H.i'); ?></span> <span class="month"><?php comments_number();?></span> </div>
<p><?php content('100'); ?></p>
</li>
<?php endwhile; ?>
<?php remove_filter( 'posts_where', 'filter_where' ); wp_reset_query(); ?>