Join Eventbrite_Query with main query
-
Hi,
I am trying to have the posts and events to show in the same feed. Basically so that the events show on the posts page, or create a custom template where they are both together.
As the events are not a custom post type, I can’t filter it through the WP_Query ‘post_type’. (or can I?)
I tried the following, but it doesn’t work:
// Set up and call our Eventbrite query. $events = new Eventbrite_Query( apply_filters( 'eventbrite_query_args', array( 'display_private' => true, // boolean ) ) ); $posts = new WP_Query( array( 'post_type' => 'post' ) ); $query = new WP_Query(); $query->posts = array_merge( $events, $posts ); $query->post_count = $events->post_count + $posts->post_count;
I also tried get_posts + eventbrite_get_events, but I am not as familiar with the get_posts function (or the latter for that matter). If anyone could help me out on this, I would be truly appreciated.
-
Hi FliperSClub,
You’ve got the right idea, merging posts and events that you’ve fetched; when you say “it didn’t work”, what were the results?
Thank you so much for your reply.
It does print 3 “articles” (I have got 2 posts and 1 event). But with the snippet below it doesnt show anything – it prints the divs and all but not its “content”:
<?php // Set up and call our Eventbrite query. $events = new Eventbrite_Query( apply_filters( 'eventbrite_query_args', array( 'display_private' => true, // boolean ) ) ); $posts = new WP_Query( array( 'post_type' => 'post' ) ); $query = new WP_Query(); $query->posts = array_merge( $events, $posts ); $query->post_count = $events->post_count + $posts->post_count; if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); if( eventbrite_is_event() ): ?> <article id="event-<?php the_ID(); ?>" <?php post_class(); ?>> <header class="entry-header"> <?php the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?> <?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it. the_post_thumbnail( 'big_feature_image' ); } ?> <div class="entry-meta"> <?php eventbrite_event_meta(); ?> </div><!-- .entry-meta --> </header><!-- .entry-header --> <div class="entry-content"> <?php eventbrite_ticket_form_widget(); ?> </div><!-- .entry-content --> <footer class="entry-footer"> <?php eventbrite_edit_post_link( __( 'Edit', 'eventbrite_api' ), '<span class="edit-link">', '</span>' ); ?> </footer><!-- .entry-footer --> </article><!-- #post-## --> <?php else: ?> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <header class="entry-header"> <?php the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?> <?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it. the_post_thumbnail( 'big_feature_image' ); } ?> <?php if ( 'post' === get_post_type() ) : ?> <div class="entry-meta"> <?php unibuddies_posted_on(); ?> </div><!-- .entry-meta --> <?php endif; ?> </header><!-- .entry-header --> <div class="entry-content"> <?php the_content( sprintf( /* translators: %s: Name of current post. */ wp_kses( __( 'Read more %s <span class="meta-nav fa fa-arrow-right"></span>', 'unibuddies' ), array( 'span' => array( 'class' => array() ) ) ), the_title( '<span class="screen-reader-text">"', '"</span>', false ) ) ); ?> <?php wp_link_pages( array( 'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'unibuddies' ), 'after' => '</div>', ) ); ?> </div><!-- .entry-content --> <footer class="entry-footer"> <?php unibuddies_entry_footer(); ?> </footer><!-- .entry-footer --> </article><!-- #post-## --> <?php endif; ?> <?php endwhile; // Previous/next post navigation. eventbrite_paging_nav( $events ); else : // If no content, include the "No posts found" template. get_template_part( 'content', 'none' ); endif; // Return $post to its rightful owner. wp_reset_postdata(); ?>
I use a conditional because of the eventbrite widget and metas.
EDIT:
FYI, I did a var_dump of the $query and got this:
object(WP_Query)#2950 (56) { ["query"]=> NULL ["query_vars"]=> array(0) { } ["tax_query"]=> NULL ["meta_query"]=> bool(false) ["date_query"]=> bool(false) ["queried_object"]=> NULL ["queried_object_id"]=> NULL ["request"]=> NULL ["posts"]=> NULL ["post_count"]=> int(3) ["current_post"]=> int(-1) ["in_the_loop"]=> bool(false) ["post"]=> NULL ["comments"]=> NULL ["comment_count"]=> int(0) ["current_comment"]=> int(-1) ["comment"]=> NULL ["found_posts"]=> int(0) ["max_num_pages"]=> int(0) ["max_num_comment_pages"]=> int(0) ["is_single"]=> bool(false) ["is_preview"]=> bool(false) ["is_page"]=> bool(false) ["is_archive"]=> bool(false) ["is_date"]=> bool(false) ["is_year"]=> bool(false) ["is_month"]=> bool(false) ["is_day"]=> bool(false) ["is_time"]=> bool(false) ["is_author"]=> bool(false) ["is_category"]=> bool(false) ["is_tag"]=> bool(false) ["is_tax"]=> bool(false) ["is_search"]=> bool(false) ["is_feed"]=> bool(false) ["is_comment_feed"]=> bool(false) ["is_trackback"]=> bool(false) ["is_home"]=> bool(false) ["is_404"]=> bool(false) ["is_embed"]=> bool(false) ["is_comments_popup"]=> bool(false) ["is_paged"]=> bool(false) ["is_admin"]=> bool(false) ["is_attachment"]=> bool(false) ["is_singular"]=> bool(false) ["is_robots"]=> bool(false) ["is_posts_page"]=> bool(false) ["is_post_type_archive"]=> bool(false) ["query_vars_hash":"WP_Query":private]=> bool(false) ["query_vars_changed":"WP_Query":private]=> bool(true) ["thumbnails_cached"]=> bool(false) ["updated_term_meta_cache"]=> bool(false) ["updated_comment_meta_cache"]=> bool(false) ["stopwords":"WP_Query":private]=> NULL ["compat_fields":"WP_Query":private]=> array(2) { [0]=> string(15) "query_vars_hash" [1]=> string(18) "query_vars_changed" } ["compat_methods":"WP_Query":private]=> array(2) { [0]=> string(16) "init_query_flags" [1]=> string(15) "parse_tax_query" } }
Many thanks in advance.
Ah, this line is probably mucking things up:
$query = new WP_Query();
You’re creating a basic query object (with all of its properties), and then swapping out its results with unrelated posts; I’m imagining the mismatch between posts and properties is causing problems.
I’d remove that line, then just merge in the events into your
$posts
object (adjustingpost_count
and other properties as you have). That should get you content showing up, and then you’ll need to verify that the filters in theEventbrite_Query
constructor are working properly (I think they should as-is, but could be wrong).Thank you so much. It is displaying them all now – however I’ve run into two new problems..
The events are now showing before the posts, and I’d like them all to be ordered (interlocked) by date… (is it possible to display by the date they were posted, rather than when the event is happening?) This is the code I have:
$events = new Eventbrite_Query( apply_filters( 'eventbrite_query_args', array( 'display_private' => true, // boolean ) ) ); $query = new WP_Query( array( 'post_type' => 'post' ) ); $query->posts = array_merge( $events->posts, $query->posts ); $query->post_count = $events->post_count + $query->post_count;
On another note, it seems the Eventbrite_Query contruct is removing the paragraphs with
remove_filter( 'the_content', 'wpautop' );
For now, I have echoed the content with the function:echo wpautop( get_the_content( sprintf( wp_kses( __( 'Read more %s <span class="meta-nav fa fa-arrow-right"></span>', 'unibuddies' ), array( 'span' => array( 'class' => array() ) ) ), the_title( '<span class="screen-reader-text">"', '"</span>', false ) ) ) );
Is it necessary for it to be in the plugin?
Thanks again!
Great, making progress ??
Regarding
wpautop
, there is already an issue for it, but ripping it out might adversely affect others, so we’ve just been suggesting to add it back in for now if you want it (see the last comment).For the order of the posts, I think you’re going to have to do some sorting after the
array_merge
. I’m pretty sure you won’t be able to sort directly on the date format, but what you can probably do is first convert them to Unix timestamps (which is numerical). That should let you sort the array based on the value of that key somehow (see PHP’s array sorting options for ideas).
- The topic ‘Join Eventbrite_Query with main query’ is closed to new replies.