I need to display the first data acf field available from tomorrow.
For example:
I have 5 post with this data: 18/02, 20/02, 21/02, 25/02, 27/02, 28/02
Today: 18/02 => I need to display post with data 20/02 (> of today but first available)
Tomorrow: 19/02 => I need to display post with data 20/02 (> of tomowwor but first available)
20/02 => I need to display post with data 21/02 (> of data but first available)
I have this loop but I don't understand where I can insert this condition and how:
<?php
$today = date('Ymd'); // Today's date
$data = get_field(data_post_available);
$args = array(
'category_name' => 'post_available',
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => 'data_post_available',
'orderby' => 'meta_value_num',
'offset' => 1
);
?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif; ?>
Can you help me?
Related
I use WP_Query to get posts by comment count
$aArgs = [
'posts_per_page' => 20,
'post_status' => 'publish',
'post_type' => ['post'],
'paged' => 1,
'orderby' => 'comment_count'
];
$oQuery = new \WP_Query($aArgs);
All posts that have comment count greater than 0 are ordered correctly. But in some posts(have 0 comment) display in at least 2 pages(Ex:paged=1 and paged=3). I dont know why. Please tell me how to fix this problem?
As I can't see your loop, I can't really guess what is going on... The following script should enable you to loop through posts via the comments count, I just tested it, and it's working on my end.
If I had to guess what is wrong with your script I would say that you probably didn't reset the post data wp_reset_postdata(); which is probably causing some troubleshoot between pages.
<?php
$query = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => 20,
'orderby' => 'comment_count'
) );
if( $query->have_posts() ):
while( $query->have_posts() ):
$query->the_post();
echo the_title() . '<br/>';
endwhile;
endif;
wp_reset_postdata(); ?>
References
Learn more about wp_reset_postdata # https://developer.wordpress.org/reference/functions/wp_reset_postdata/
I am trying to feed in a tax_query argument with a variable pulling from a Custom Field. Yet nothing is returned. I did a var_dump on my variable and it keeps returning "boolean:False". So I thought maybe I needed to run a loop (my custom field is a sub_field of a group), and then var_dump returns nothing at all. I'm fairly new to WP/PHP development, not sure what's going on here. Any help would be greatly appreciated!
<?php
if(have_rows('wine_profile')) :
while(have_rows('wine_profile')) : the_row();
$label = get_sub_field("taxonomy_label");
var_dump($label);
$wineProfiles = new WP_Query(array(
'posts_per_page' => '-1',
'post_type' => 'wines',
'tax_query' => array(
array(
'taxonomy' => 'labels',
'field' => 'slug',
'terms' => $label
)
),
'order' => 'DESC',
'orderby' => 'ID'
));
if ($wineProfiles->have_posts()) : while ($wineProfiles->have_posts()) : $wineProfiles->the_post();
get_template_part('includes/component', 'colorBlockLg');
get_template_part('includes/component', 'profile');
?>
<?php endwhile;
endif; endwhile; endif; ?>
Try to use -
$label = get_sub_field("taxonomy_label",get_the_ID());
var_dump($label);
I guess because you haven't started post loop!
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Put your code here //
} // end while
} // end if
?>
``
I am trying to get this work, but it's not entirely working. This is how I currently query my posts:
<?php
// the query
$the_query = new WP_Query( array( 'posts_per_page' => -1 ) );
if ( $the_query->have_posts() ) :
?>
<!-- pagination here -->
<!-- the loop -->
<?php
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<li data-href="<?php $zlink = get_the_permalink(); echo preg_replace("#/$#im", '', $zlink);?>">
<div>
<a class="button" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</div>
What I am trying to achieve, is the following:
Show recently commented post on top of posts that have a newer publish date...
If a blog-post has a comment, regardless of how old that blog-post is, I want that blog-post above a newer blog-post, if that newer one has no update (meaning: no recent comments).
I started by showing the recently commented posts on top (see below), but this totally neglects the fact that there are posts without comments and I can't find a way to combine both and show them in one list.
<?php
$args = array(
'status' => 'approve',
'number' => 6,
'order' => 'DESC'
);
$comments = get_comments($args);
foreach($comments as $comment) : $count++;
$post_args = array(
'post_type' => 'post',
'p' => $comment->comment_post_ID,
'posts_per_page' => 1
);
$posts = get_posts($post_args);
foreach($posts as $post) : setup_postdata($post);
the_title();
endforeach;
endforeach;
?>
Could someone help?
The below creates an empty array, adds all the posts with comments to that array. It then adds all the posts without comments. Finally, it sorts them by the comment date or the post date, depending on whether that post had any comments or not
//create an array to stuff all your posts in
$arrAllPosts = array();
$args = array(
'post_type' => 'post', //show only posts (not pages, etc)
'comment_count' => array( //pass it an array
'value' => 1, //value is 1, with compare means greater than or equal to 1
'compare' => '>='
),
'posts_per_page' => -1 //gimme all of them
);
$postsWithComments = new WP_Query($args);
while($postsWithComments->have_posts()) {$postsWithComments->the_post();
//get the comments for this post
$comments = get_comments(array(
'post_id' => get_the_ID(), //pass the post ID
'orderby' => 'comment_date', //tell it to sort by the comment date, so you only get the latest
'number' => 1 //just get the latest
));
foreach($comments as $comment) { //we're only looping this once, since there is only one
$arrAllPosts[] = array(
'dateToSortBy' => $comment->comment_date, //we'll use comment date to sort by later, instead of the post date
'the_post_obj' => $post //add the global post object, which is currently set to the current post
);
}
}
//now we get the posts with no comments
$args = array(
'post_type' => 'post', //Only posts (not pages, etc)
'comment_count' => 0, //Posts with no comments only
'posts_per_page' => -1 //gimme all of them
);
$postsNoComments = new WP_Query($args); //run it
while($postsNoComments->have_posts()) {$postsNoComments->the_post(); //loop it
$arrAllPosts[] = array(
'dateToSortBy' => $post->post_date, //we'll use the post date to sort by
'the_post_obj' => $post //add the global post object, which is currently set to the current post
);
}
function date_compare($a, $b) { //create a custom function to sort the array by the date of the post or the date of the comment
$tmA = strtotime($a['dateToSortBy']); //convert to time
$tmB = strtotime($b['dateToSortBy']); //convert to time
return ($tmA < $tmB)?true:false;
}
usort($arrAllPosts, 'date_compare');
//Display the title for each post, from the sorted list
foreach($arrAllPosts as $curPost) {
$post = $curPost['the_post_obj']; //make this the global post object
setup_postdata($post); //setup the data
echo "<a href='" . get_the_permalink() . "'>" . get_the_title() . "</a>";
}
Newbie here...
I have a custom post type of 'equipe' (team in portuguese). I am trying to sort these alphabetically by post title then display the_title so we have a alphabetical list of names.
I've done a search on here and tried a few fixes but Im struggling to get anything other that the standard order.
Any help would be much appreciated!
<?php
$args = array('orderby'=> 'title', 'order' => 'ASC', 'post_type' => 'equipe', 'posts_per_page' => -1, 'post_status' => 'publish' );
$q = new WP_Query($args);
while ( $q->have_posts() ) : $q->the_post();
?>
<h3><?php the_title(); ?></h3>
<?php
endwhile;
wp_reset_query();
?>
<?php
$args = array( 'post_type' => 'equipe', 'posts_per_page'=>5, 'orderby'=>'post_title','order'=>'ASC');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
RESOLVED:
Ok the reason it was enforcing menu_order was because of a setting (F*ing checkbox) within the plugin Post Types Order.
I needed to un-check AUTO SORT
and check Use query ASC / DESC parameter
This then allowed me to adjust the array as follow (and discussed above):
$args = array('orderby' => 'title', 'order'=>'ASC', 'post_type' => 'equipe')
However I did need to add 'order'=>'ASC' into the other pages that sorted by the original query of menu_order.
I am having trouble with a custom Wordpress / ACF loop I am working on.
The idea is that it displays the latest posts within the 'events' post type, hiding any posts where the event date has passed.
The posts do hide if the date has passed. However the loop is not displaying the full amount of posts available. Currently with the loop below, it is only showing 6 out of the available 10.
I have checked the reading settings in Wordpress and that's fine.
The code I am using for my loop is:
<ul class="events-list">
<?php
$loop = new WP_Query( array(
'post_type' => 'events',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_type' => 'DATE',
'meta_key' => 'event-date'
));
while ( $loop->have_posts() ) : $loop->the_post();
$today = date('dmY');
$expire = get_field('event-date');
if( $expire > $today )
{ ?>
<li>
<h3><?php the_field('event-date'); ?> - <?php the_title(); ?></h3>
<span class="time"><?php the_field('event-time'); ?></span>
<?php the_field('event-details'); ?>
</li>
<?php; } endwhile; wp_reset_query(); ?>
</ul>
If you're going to compare dates then you need to convert them to the appropriate types. Convert them to Unix timestamp then you can easily compare when the date has surpassed. At the moment you are comparing which string is greater than the other which works sometimes but it's much more reliable to use Unix timestamp as your date formats always need to match.
if(strtotime(get_field('event-date')) > date('U')) {
//Your code here
}
Simply print compared dates before the "if" statement, and you will see where you made a mistake.
echo $expire.'__'.$today.'<br>';
if( $expire > $today )
It can be because of invalid date format, empty $expire field etc.. Anyway, you will see what the reason is after implementing that print.
The solution to this problem was to change the loop to:
<?php
$today = date('Ymd');
$loop = new WP_Query( array(
'post_type' => 'events',
'showposts' => 2,
'meta_key' => 'event-date',
'meta_compare' => '>',
'meta_value' => date("Ymd"),
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
while ( $loop->have_posts() ) : $loop->the_post();
{ ?>
Post stuff here
<?php; } endwhile; wp_reset_query(); ?>