Wordpress Event Organiser Pagination - wordpress

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

Related

Get multiple custom taxonomy term names in a query loop - Wordpress

I'm facing an issue with my website.
I would like to display all my custom post type named "Projets", and for each item, I want to get several term names to put in my data element.
Displaying all my post is not an issue, it's working well. I manage to display one term name using "get_terms()", but I don't know how to display several terms and put them in the right place.
I have 3 different custom taxonomy : city, typo and statut.
There is my code :
<?php
$args = array(
'post_type' => 'projets',
'posts_per_page'=>'99',
'order' => 'ASC',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li class="content_item active" data-city="CITY_NAME_HERE" data-typo="TYPO_NAME_HERE" data-statut="STATUT_NAME_HERE">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('custom-size', ['class' => 'content_item_img', 'title' => 'Image du projet']); ?>
<h4><?php the_title(); ?></h4>
</a>
</li>
<?php endwhile;
wp_reset_postdata();
?>
Thanks for the help!
Try this:
Inside the loop, you get the taxonomy like this (eq. "city"):
$cities = get_terms( array(
'taxonomy' => 'city',
'hide_empty' => false,
) );
You get an array of all the terms of the Taxo.
Where you need them, you have to loop them, here to give you an idea.
if(!empty($cities)){
foreach($cities as $city){
echo $city->name.', ';
}
}else{
echo 'No Cities.';
}
Comma Separated custom categories inside the loop.
<?php echo get_the_term_list( $post->ID, 'cutom_category', '', ', ', '' ) ?>

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.

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: not stops or resets query

I am adding post thumbnail jquery slider to header part but it's resulting weird issue. It is somehow not ending while or stop query and so if I will go to single post or page it is keep displaying loop instead of page or post content.
I have tried two different query but none of them stopping to this weird issue.
First Tried
<?php
query_posts( 'post_status=publish&orderby=rand' );
while (have_posts()) : the_post();
$title_attr = array(
'title' => get_the_title(),
'alt' => get_the_title(),
'class' => get_the_title(),
);
echo '<a href="#post-'.get_the_ID().'" class="scroll theme">';
the_post_thumbnail('thumbnail',$title_attr);
echo '</a>';
endwhile; ?>
Than Second Tried
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('post_status=publish&orderby=rand');
// The Loop
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
$title_attr = array(
'title' => get_the_title(),
'alt' => get_the_title(),
'class' => get_the_title(),
);
echo '<a href="#post-'.get_the_ID().'" class="scroll theme">';
the_post_thumbnail('thumbnail',$title_attr);
echo '</a>';
endwhile; endif; wp_reset_query();?>
None of these stopping to display loop ( all post like index page) in to single post or page.
I will go to single post or page it is keep displaying loop instead of page or post content...
That is because you are not passing it any parameters that would limit the query for single posts. Your query ( $wp_query->query('post_status=publish&orderby=rand'); ) pulls all posts, all the time, and in random order. For single post display you need to pass it a post or page parameter. You probably need to use get_query_var() to check for 'p', 'page_id', or both. Something like this:
$pid = get_query_var('p');
if (!empty($pid)) {
$qry = 'p='.$pid;
} else {
$qry = 'post_status=publish&orderby=rand';
}
$wp_query->query($qry);
There are other possible solutions as well, like is_single().
Also, WordPress uses the variable $wp_query so you should really pick another one instead of clobbering that one.
I found the solution :)
I have added if (have_posts()) before while and end loop with wp_reset_query() and all good now. :)
So here is final code if anyone having same problem..
<?php
query_posts( 'post_status=publish&orderby=rand' );
if ( have_posts()): while (have_posts()) : the_post();
$title_attr = array(
'title' => get_the_title(),
'alt' => get_the_title(),
'class' => get_the_title(),
);
echo '<a href="#post-'.get_the_ID().'" class="scroll theme">';
the_post_thumbnail('thumbnail',$title_attr);
echo '</a>';
endwhile; endif; wp_reset_query(); ?>

Resources