"Show only first instance of recurring event" alters query for pages too?!

Home Forums Calendar Products Events Calendar PRO "Show only first instance of recurring event" alters query for pages too?!

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1296789
    Sarah
    Participant

    Hi, we have a site that is using AJAX to query the database based on user input. We are displaying multiple types of content, including pages, posts, custom post types and tribe_events. Like most users, we do not want every single future instance of recurring events to show up in the results, so we have checked the box in tribe settings to “Show only the first instance of each recurring event”
    However when we set this, it seems to modify the query so that it only shows the first instance of CHILD PAGES as well.

    For example, we have about two dozen “sibling” child pages showing all different kinds of services. Our AJAX query shows all of them mixed with other results as expected when the “show only first instance” setting is NOT checked. (but as you can imagine, future events totally dominate this list which is annoying) But when this settings box is checked, the results of our AJAX query shows only the ONE most recent CHILD PAGE as well as tribe_event. (I tested this by changing the modified date of the page).

    This setting is incredibly heavy handed — it really needs to be changed to ONLY affect tribe_event post types! Is there any way we can alter this behavior?

    #1298099
    Barry
    Member

    Hi Amanda,

    Sorry to hear you’ve hit up against some difficulties and our apologies for the delay.

    I’d love to better understand the problem. It sounds like you are doing something via custom code here – is that correct and can you (privately if you prefer) share the relevant snippets?

    Certainly if I set up a test whereby I create a frontend script that requests a list of posts via ajax, and the listener code performs a simple WP_Query that targets both pages and events and returns the result, I don’t seem to encounter the same issue: pages and their child pages are returned, not only the first child page – irrespective of the value of the setting you mentioned.

    With that in mind I’d love to build up a better picture of what you are doing and how you are doing it, if at all possible.

    Thanks!

    #1298503
    Sarah
    Participant

    Hi Barry thanks so much for your reply, I’ll be happy to give more detail.

    An example of the page that we are talking about is here: http://whtest2.wizzywigwebdesign.com/ministry/education-discipleship/ (others can be found by going to http://whtest2.wizzywigwebdesign.com/get-involved/whc-ministries/ and selecting any ministry)

    This is a taxonomy archive for a custom taxonomy called “Ministry”. This taxonomy can be applied to multiple post types, including posts, pages, tribe_events, a CPT called “support_groups” and a CPT called “volunteer_opps” and one called “classes”.

    If you go to the archive for this term: http://whtest2.wizzywigwebdesign.com/ministry/care/ and look at the pages that appear, then if you tick the “page” box to filter just pages, you’ll see there are actually many more pages than are being displayed here. When the box is not checked (ie should be showing ALL post types incl. pages) it only shows the ONE most recent child page (of a parent page called “Find Support”).

    We have a custom theme and the template used on this page is using ajax to populate the page (based on the selected post types that the user has chosen to filter for). The taxonomy-ministry.php theme file itself is pretty vanilla, nothing fancy, other than some checkboxes to allow users to filter post type:

    Filter by:

    <div id=”ministry_filters”>
    <label><input type=”checkbox” name=”volunteer_opp”> Volunteer Opportunities</label>
    <label><input type=”checkbox” name=”page”> Pages</label>
    <label><input type=”checkbox” name=”post”> Posts</label>
    <label><input type=”checkbox” name=”tribe_events”> Events</label>
    <label><input type=”checkbox” name=”class”> Classes</label>
    <label><input type=”checkbox” name=”support_group”> Support Groups</label>
    </div>

    The custom code comes in via the following script in our functions.php file.

    (Note that this issue overlaps an issue regarding a database error when the “show only first instance” is checked, which is still awaiting support in this thread.)

    // ---------------- Filter Ministries ------------------
    add_action( 'wp_ajax_filter_ministry', 'filter_ministry' );
    add_action( 'wp_ajax_nopriv_filter_ministry', 'filter_ministry' );
    
    function filter_ministry(){
        global $wpdb; // access to the database
    
        $paged = ( isset($_REQUEST['paged'] ) ) ? $_REQUEST['paged'] : 1;
    
        $args = array(
            'posts_per_page'         => '15',
            'paged' => $paged,
            'post_status' => 'publish',
        );
    
        if (isset($_REQUEST['post_type']) and !empty($_REQUEST['post_type'])){
            $args['post_type'] = array_keys($_REQUEST['post_type']);
        }
        else{
            $args['post_type'] = array('post','page','volunteer_opp','tribe_events','class','support_group');
        }
        #$args['tax_query'] = array();
        $args['tax_query'] = array(
                array(
            'taxonomy'  => 'ministry',
            'field'   => 'slug',
            'terms'   => array($_REQUEST['term']),
            'operator' => 'IN'
            )
        );
    
        if (count($args['post_type'])>1 and in_array('tribe_events',$args['post_type'])){
            $args['meta_query'] = array(
                    'relation' => 'OR',
                    'end_date_clause' => array(
                            'key' => '_EventStartDate',
                            'value' => date("Y-m-d H:i:s"),
                            'compare' => '>'
                    ),
                    'date_exist_clause' => array(
                            'key' => '_EventStartDate',
                            'compare' => 'NOT EXISTS'
                    )
            );
        }
    
        $blogquery = new WP_Query( $args );
    
        ob_start();
        // The Loop
        if ( $blogquery->have_posts() ) {
            while ( $blogquery->have_posts() ) {
                $blogquery->the_post();
                if(get_post_type() != "staff_member") { // we don't display staff members in this list ?>
                    <article class="excerpt clearfix">
                        
                        <h2><a>"><?php the_title(); ?></a></h2>
    
                        <h4>
    	                    <?php 
    	                    $mytype = get_post_type();
    	                    $obj=get_post_type_object($mytype);
    						echo $obj->labels->singular_name;
    						?>
                        <?php if(get_post_type() != 'tribe_events') { //just show the post date for posts
    	                    		if(get_post_type() == 'post') { ?>
    	                    			  &bull; <?php wizzy_wig_posted_on(); ?>
    		                    	<?php } ?>
    		                    
    		                <?php } else { // show the event date if it's an event
    			                if (function_exists('tribe_get_start_date')) {
    		                    	echo ' &bull; ' . tribe_get_start_date() ;
    		                    }
    		                }
    	                ?>
    	                </h4>   
                        
                        <?php the_excerpt(); ?>
                        <hr />
                    </article>
        <?php } ?>
            <?php }
        }
        else{
            echo '<h1>Nothing found</h1>';
        }
    
        matword_pagination2($blogquery);
    
        $res = ob_get_contents();
        ob_end_clean();
        echo $res;
        exit;
    }
    
    // ---------------- The pagination script referenced in the function above ------------------
    function matword_pagination2($blogquery) {
    
        $prev_arrow = is_rtl() ? '&rarr;' : '&larr;';
        $next_arrow = is_rtl() ? '&larr;' : '&rarr;';
    
        $total = $blogquery->max_num_pages;
        $big = 999999999; // need an unlikely integer
        if( $total > 1 )  {
             if( !$current_page = $_REQUEST['paged'] )
                 $current_page = 1;
             if( get_option('permalink_structure') ) {
                 $format = 'page/%#%/';
             } else {
                 $format = "&paged=%#%";
             }
            echo paginate_links(array(
                'base'          => htmlspecialchars_decode(  str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ) ),
                'format'        => $format,
                'current'       => max( 1, $_REQUEST['paged'] ),
                'total'         => $total,
                'mid_size'      => 3,
                'type'          => 'list',
                'prev_text'     => $prev_arrow,
                'next_text'     => $next_arrow,
             ) );
        }
    }
    #1298756
    Barry
    Member

    Thanks for sharing, however I still don’t see the problem.

    I setup that code locally (though I removed the taxonomy query to avoid having to set that up for different post types). On triggering an ajax request the query functioned as expected. In my case, the results included a parent page plus both of its children – again, regardless of the status of the “Show only the first instance of each recurring event” setting.

    So, while that doesn’t conclusively prove there isn’t a problem it isn’t one I can reproduce based on the information at hand. Also, given this also relates to custom code, I’m sorry to say we may not be able to take this much further.

    One last thing that might help, if you’re able to obtain it, is a copy of the generated SQL that is executed (ie, $blogquery->request). If you can share that we might be able to further pin this down.

    Thanks!

    #1298787
    Sarah
    Participant

    Sure, here is the var_dump of blogquery… I am wondering if there is a clue in the last couple of lines, where it says “group by” — this query changes based on the settings of the “Show First Instance” box.

    CHECKED:

    string(1116) "
    			SELECT
    				SQL_CALC_FOUND_ROWS *
    			FROM (
    				SELECT  DISTINCT whc2_posts.*, IF (whc2_posts.post_type = 'tribe_events', tribe_event_postmeta.meta_value, whc2_posts.post_date) AS EventStartDate FROM whc2_posts  LEFT JOIN whc2_term_relationships ON (whc2_posts.ID = whc2_term_relationships.object_id) LEFT JOIN whc2_postmeta ON ( whc2_posts.ID = whc2_postmeta.post_id )  LEFT JOIN whc2_postmeta AS mt1 ON (whc2_posts.ID = mt1.post_id AND mt1.meta_key = '_EventStartDate' ) LEFT JOIN whc2_postmeta as tribe_event_postmeta on whc2_posts.ID = tribe_event_postmeta.post_id AND tribe_event_postmeta.meta_key = '_EventStartDate' WHERE 1=1  AND ( 
      whc2_term_relationships.term_taxonomy_id IN (19)
    ) AND ( 
      ( whc2_postmeta.meta_key = '_EventStartDate' AND whc2_postmeta.meta_value > '2017-06-15 21:02:45' ) 
      OR 
      mt1.post_id IS NULL
    ) AND whc2_posts.post_type IN ('post', 'page', 'volunteer_opp', 'tribe_events', 'class', 'support_group') AND ((whc2_posts.post_status = 'publish')) ORDER BY post_date DESC 
    			) a
    			GROUP BY IF( post_parent = 0, ID, post_parent )
    			ORDER BY EventStartDate DESC
    			LIMIT 0, 15
    		"


    UN-CHECKED:

    string(1005) "SELECT SQL_CALC_FOUND_ROWS DISTINCT whc2_posts.*, IF (whc2_posts.post_type = 'tribe_events', tribe_event_postmeta.meta_value, whc2_posts.post_date) AS post_date FROM whc2_posts  LEFT JOIN whc2_term_relationships ON (whc2_posts.ID = whc2_term_relationships.object_id) LEFT JOIN whc2_postmeta ON ( whc2_posts.ID = whc2_postmeta.post_id )  LEFT JOIN whc2_postmeta AS mt1 ON (whc2_posts.ID = mt1.post_id AND mt1.meta_key = '_EventStartDate' ) LEFT JOIN whc2_postmeta as tribe_event_postmeta on whc2_posts.ID = tribe_event_postmeta.post_id AND tribe_event_postmeta.meta_key = '_EventStartDate' WHERE 1=1  AND ( 
      whc2_term_relationships.term_taxonomy_id IN (19)
    ) AND ( 
      ( whc2_postmeta.meta_key = '_EventStartDate' AND whc2_postmeta.meta_value > '2017-06-15 21:05:56' ) 
      OR 
      mt1.post_id IS NULL
    ) AND whc2_posts.post_type IN ('post', 'page', 'volunteer_opp', 'tribe_events', 'class', 'support_group') AND ((whc2_posts.post_status = 'publish')) GROUP BY whc2_posts.ID ORDER BY post_date DESC LIMIT 0, 15"
    #1298852
    Barry
    Member

    OK! Thanks for persevering.

    With some tweaks to my test data, I can see this problem. An ‘easy’ solution would be to turn off the “Show only the first instance of each recurring event” setting only for this query, via a further query action/filter that does something along these lines:

    $wp_query->set( 'tribeHideRecurrence', 0 );

    Would that work for you, or do you need this setting in place here, too (but targeting only events)? If that is the case, I’m not too sure we have any great workarounds for you to use, unfortunately.

    #1299402
    Sarah
    Participant

    Great glad we are getting somewhere! Unfortunately we really do need the events to be grouped, since we have some like this which just totally dominate the list, otherwise.

    Can you think of any other workaround? Like to have that grouping occur ONLY on tribe_events post types? (that is how this really should be operating anyway) Perhaps there is some way we could turn off the global grouping setting and instead write it into our query manually so that it only affects events? (not sure how we’d write that, but in theory it seems like that should be possible)
    Thanks a lot for your help!

    #1299408
    Barry
    Member

    Hi Amanda!

    I agree that’s how it should work (ie, it should impact events only) but, unfortunately, that isn’t the case and the current solution – unfortunately – has these side effects.

    Perhaps there is some way we could turn off the global grouping setting and instead write it into our query manually so that it only affects events?

    That is the sort of path I’d recommend here. Again, you can set tribeHideRecurrence to a zero or false for the query and turn this off – but it does mean you’d need to figure out an alternative means of achieving the same goal.

    If it needs to work across a paginated result set that may be challenging; on the other hand if it so happens you are only ever concerned with a single page of results you could probably perform the manipulation via PHP.

    I’m afraid, though, that further direct assistance on this one is beyond what we can offer here in the support forums – though I did make a note on a related internal feature ticket of the problem you described, so hopefully the dev team can factor that in as we continue to evolve our recurring event functionality.

    #1317942
    Support Droid
    Keymaster

    Hey there! This thread has been pretty quiet for the last three weeks, so we’re going to go ahead and close it to avoid confusion with other topics. If you’re still looking for help with this, please do open a new thread, reference this one and we’d be more than happy to continue the conversation over there.

    Thanks so much!
    The Events Calendar Support Team

Viewing 9 posts - 1 through 9 (of 9 total)
  • The topic ‘"Show only first instance of recurring event" alters query for pages too?!’ is closed to new replies.