ACF Date Picker Field Show Only the Current Post Month (Not Older Post at This Month) - wordpress

<?php
// Find todays date in Ymd format.
$today = date('Ymd');
// Query posts using a meta_query to compare two custom fields; start_date and end_date.
$args = array(
'post_type' => 'special-offer',
'meta_query' => array(
array(
'key' => 'special-offer-start_date',
'compare' => '>=', // This line how to limit it on this month?
'value' => $today,
'type' => 'DATE',
),
));
// query
$the_query = new WP_Query( $args );?>
<?php if( $the_query->have_posts() ): ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2> <?php the_title(); ?> </h2>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
Hello, Guys! Anyone help me? to my problem i came with this because acf took long to reply. my problem is i want to show the posts within this month not on the future event. i already try the start date and end date with acf document but seem is not working. .
please help me thank you

Related

Custom WP_Query won't print items to page, even though it contains the expected data

I have a WP_Query which contains data that I want to loop over on the page:
$query_posts_for_board_game = new WP_Query(get_posts(array(
'post_type' => $mm_custom_post_types,
'numberposts' => 20,
'meta_query' => array(
array(
'key' => array('board_game', 'board_games'),
'value' => get_the_ID(),
'compare' => 'LIKE'
)
)
)));
When I var_dump it I can see that it has the data in the query and query_vars properties but when I loop over it using the $query_posts_for_board_game->have_posts() method nothing is output. This code simply prints the else block.
<?php if($query_posts_for_board_game->have_posts()): ?>
<?php while ($query_posts_for_board_game->have_posts()) : $query_posts_for_board_game->the_post(); ?>
<?php get_template_part('template-parts/layouts/content', 'b1' ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part('template-parts/layouts/content-none' ); ?>
<?php endif;?>
If I remove the call to WP_Query and just use get_posts I'm able to loop over it with a standard for loop, but then the nested templates can't take advantage of $post like they would with a normal loop:
<?php
// If there are posts
if ($posts_for_board_game) :
// Loop the posts
foreach ($posts_for_board_game as $board_game_post) :
?>
<?php echo $board_game_post->post_title . '<br />'; ?>
<?php
endforeach;
wp_reset_postdata();
?>
<?php endif; ?>
I seem to recall that this is because my custom query isn’t part of “the query”. Is there a way I can override “the query” so that my content can be output? Can I simply move my query further down the page after the other items are output?
I solved it! I'm sort of shocked that it was as easy as this.
I left the WP_Query in place, just like in the first query above. Then in my loop I simply changed the name of the iterator to $post and it started working. Here's the complete code for anyone else who has this question.
$query_posts_for_board_game = new WP_Query(get_posts(array(
'post_type' => $mm_custom_post_types,
'numberposts' => 20,
'meta_query' => array(
array(
'key' => array('board_game', 'board_games'),
'value' => get_the_ID(),
'compare' => 'LIKE'
)
)
)));
$posts_for_board_games = $query_posts_for_board_game->query;
and the output:
<?php if($posts_for_board_games): ?>
<?php foreach ($posts_for_board_games as $post): ?>
<?php get_template_part('template-parts/layouts/content', 'b1' ); ?>
<?php
endforeach;
wp_reset_postdata();
?>
<?php else: ?>
<?php get_template_part('template-parts/layouts/content-none' ); ?>
<?php endif;?>
I'm a little annoyed that I had to use an intermediate variable, to get at the query contents, but I'm fine with this code.

Wordpress Event Organiser Pagination

If anyone out there is familiar with Event Organiser templates. I could use some help. I am trying to limit it to 5 per page and add custom pagination. I can't seem to edit the loop in eo-loop-events.php file to add a custom query to limit it to 5 per page.
I've tried the shortcode and looked at their documentation all afternoon and am getting nowhere. I figure there is probably an easy way to do this and I am missing it or I can't do this with the free version.
Any help would be appreciated.
I just had to implement pagination for my wp events organiser template. This is inside widget-event-list.php but will probably work in other templates. This is copy pasted from a working example within the template.
This utilizes the WP_Query loop instead of the $eo_event_loop() that the template uses by default. The WP_Query loop has arguments to get "event" data only.
Fortunately, the event organiser functions (like eo_get_event_datetime_format()) still work.
In this example, I am only outputting the title in the <h4> tag.
This solution was inspired by this answer to another question.
<h1>Paginated Query Area</h1>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type'=>'event', // Your post type name
'posts_per_page' => 3,
'paged' => $paged,
);
$loop = new WP_Query( $args );
?>
<?php if ( $loop->have_posts() ): ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
//Generate HTML classes for this event
$eo_event_classes = eo_get_event_classes();
//For non-all-day events, include time format
$format = eo_get_event_datetime_format();
?>
<h4 class="event-title"><?php the_title(); ?></h4>
<?php endwhile; ?>
<div class="upcoming-pagination">
<?php // Pagination display
$total_pages = $loop->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
?>
</div>
<?php endif; ?>
<?php wp_reset_postdata(); ?>

Pagination shows empty pages for date based archive.php loop

I'm using wordpress and have a custom archive page for a custom post type.
The custom loop gets the logged in users registration date and only shows posts that were published on or after they registered. Works great.
However, the pagination is still using the main query so if there are 4 posts in total set to 2 posts per page the pagination always shows there are two pages even if only one post is displayed due to the logged in users registered date.
Can anyone help me modify what I have so the pagination only shows for results in more than 2 posts for that users query? I've been trying for hours now using various changes I've found on the web...
<?php if ( have_posts() ): ?>
<?php
# Get the current user's info
$user_info = get_userdata(get_current_user_id());
# Use date_parse to cast your date to an array
$regdate = date_parse($user_info->user_registered);
# Set your arguments for WP Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'inner',
'posts_per_page' => '2',
'posts_per_archive_page' => '2',
'paged' => $paged,
'date_query' => array(
array(
'after' => array(
# Setting date to array above allows to call specific values within that date
'year' => $regdate['year'],
'month' => $regdate['month'],
'day' => $regdate['day'],
),
# Include posts from the day the user registered
'inclusive' => true,
),
),
# Display all posts on a single page.
);
$my_query = new WP_Query( $args );
while ($my_query->have_posts()) : $my_query->the_post();
get_template_part( 'template-parts/content', get_post_format() );
endwhile; ?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else: ?>
Nada
<?php endif; ?>
Working with Custom Archive and Pagination
#Scott Eldo Your approach with create custom query will not change main query on your custom archive. FYI and you are correct, pagination only work for main query.
In your case, the recommended approach, I will use filter pre_get_posts to work with custom archive and pagination. Please take a look my answer here how to modify main query on post type archive page, you can figure it out with your query parameters.
BUT if you intent to create query direct into your template ( even it is not change main query ), you need to match your custom query with $GLOBALS['wp_query'] that use in pagination and don't forget to use wp_reset_query() ( MUST ). Take a look my approach here related with your code:
<?php if ( have_posts() ): ?>
<?php
# Get the current user's info
$user_info = get_userdata(get_current_user_id());
# Use date_parse to cast your date to an array
$regdate = date_parse($user_info->user_registered);
# Set your arguments for WP Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'inner',
'posts_per_page' => '2',
'posts_per_archive_page' => '2',
'paged' => $paged,
'date_query' => array(
array(
'after' => array(
# Setting date to array above allows to call specific values within that date
'year' => $regdate['year'],
'month' => $regdate['month'],
'day' => $regdate['day'],
),
# Include posts from the day the user registered
'inclusive' => true,
),
),
# Display all posts on a single page.
);
$my_query = new WP_Query( $args );
while ($my_query->have_posts()) : $my_query->the_post();
get_template_part( 'template-parts/content', get_post_format() );
endwhile; ?>
<?php
/**
* Fix Pagination in custom page/archive
* Set global wp_query the same as our custom query
*
* Use function the_posts_pagination( $args ); for pagination will work too
* see https://developer.wordpress.org/reference/functions/get_the_posts_pagination/#source-code
*/
$GLOBALS['wp_query'] = $my_query;
?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else: ?>
Nada
<?php endif; ?>
<?php wp_reset_query(); ?> // MUST use to reset global query and post data
Another approach and still NOT recommended is use query_post for your custom query. More reading about it you can learn more in here.

Wordpress - Display only posts in the past breaking pagination

I have an events page displaying an 'events' custom post type. At the top of the page I have 2 upcoming posts in an upcoming event list and then at the bottom I have 3 past events in a past events list. Posts are displayed in each section based on if an 'event date' custom field is in the past or not. The past events uses WP page navi plugin to paginate the posts. 'posts_per_page' is set at one as a test.
There should be 3 pages that have 1 past post displayed on them, respectively. Unfortunately, the pagination shows 5 pages, the first two have no posts displayed on them, as if the posts from the upcoming posts lists are behaving as ghosts. I have removed the upcoming posts list as a test but this makes no difference.
Does anyone know any reason for this? I have no idea. Thanks.
<?php
$temp_post = $post; // Store the Page the Post
// Get the current date
$current_date = date('M d, Y', time());
$current_date = strtotime( $current_date );
$wp_query= null;
$wp_query = new WP_Query();
$args = array( 'post_type' => 'events','posts_per_page' => 1,'paged' => $paged,'meta_key' => 'event_date', 'orderby' => 'meta_value_num', 'order' => 'DESC');
$wp_query->query( $args );
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
// Get the date custom field
$post_date = get_field('event_date');
$post_date = strtotime( $post_date );
// If older than post date, don't show it
if( $current_date > $post_date ): ?>
MY POST CONTENT GOES HERE
<?php endif; // END DATE FILTER ?>
<?php endwhile; //END LOOP ?>
<?php /* Start wp-pagenavi support */ ?>
<?php if(function_exists('wp_pagenavi') ) :
echo '<nav class="pag">'; ?>
<?php wp_pagenavi(); ?>
<?php echo '</nav>'; ?>
<?php else: ?>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php endif; ?><?php /* End wp-pagenavi support */ ?>
<?php endif; ?>
<?php $post = $temp_post; // Reset the Post ?>
It's acting as ghost because you are not showing it. You are showing the navigation in the bottom of page. The query returns 5 page, so navigation would be for 5 page.
But you aren't showing the post, if it doesn't meet this condition
// If older than post date, don't show it
if( $current_date > $post_date ): ?>
That's why those posts are not showing. Remove that condition and you will see that all the posts are showing as it should.
If you want to display pagination this way, you would have to limit the number of returned post from query itself.
Thanks a lot, I managed to work it out with a push in the right direction.
// Get the current date
$current_date = date('Ymd', time());
$wp_query= null;
$wp_query = new WP_Query();
$args = array(
'post_type' => 'events',
'posts_per_page' => 1,
'paged' => $paged,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'event_date',
'meta_query' => array(
array(
'key' => 'event_date',
'value' => $current_date,
'compare' => '<',
'type' => 'CHAR'
)
)
);
$wp_query->query( $args );
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
MY POST CONTENT HERE
<?php //endif; // END DATE FILTER ?>
<?php endwhile; //END LOOP ?>
<?php /* Start wp-pagenavi support */ ?>
<?php if(function_exists('wp_pagenavi') ) :
echo '<nav class="pag">'; ?>
<?php wp_pagenavi(); ?>
<?php echo '</nav>'; ?>
<?php endif; ?><?php /* End wp-pagenavi support */ ?>
<?php endif; ?>

Wordpress - Sorting post loop by meta data date

I have two CPTs, one called 'artist' and the other called 'release.' I've created a single-artist.php page that displays an artist and its' custom meta data. On that same page I am displaying all releases by that artist with the following code:
<!-- GET RELEASES -->
<?php
$category = get_the_category();
$artist_name_slug = $category[0]->slug;
$args = array ('post_type' => 'release', 'posts_per_page' => 20, 'category_name' => $artist_name_slug);
query_posts ($args);
?>
<?php if (have_posts()) : ?>
<h3 class="artist-col2-title">Releases</h3>
<?php while (have_posts()) : the_post(); ?>
<div class="artist-release"><?php echo the_post_thumbnail('small'); ?></div>
<?php endwhile; ?>
<?php endif; ?>
<div style="clear:both;"></div>
Within the release CPT I have a release date in the meta data.
I would like to sort the releases based on that date but I cannot figure out how to add that to my arguments. Any help would be greatly appreciated!
To sort by meta data you can use
$args = array (
'post_type' => 'release',
'posts_per_page' => 20,
'category_name' => $artist_name_slug,
'meta_key' => 'your_meta_key' // i.e. release_date
'orderby'='meta_value' // for numeric value use 'meta_value_num' instead
);
query_posts ($args);
But notice the meta_key should be present in the query that you want to use for sorting, see for more.

Resources