• Resolved robbiegod

    (@robbiegod)


    Recently updating wordpress and now I am trying to do accomplish something that is giving me a real headache. I was using an event calendar plugin, but now i am trying to remove that plugin and create my own events system. I’m getting hung up querying the posts by a date range.

    I registered a custom post type and then used ACF (Advanced Custom Fields) to add two Date Picker fields. One field is the Event Start Date and the other is the Event End Date. Occasionally, an event will span for 3 months and it is these types of events that I am having the most problems with.

    I have an area on my webpage that shows the events filtered by month and year. Let’s call this my Event List Widget (it’s not a wordpress widget). This is where I think things are getting tripped up because if I have an event that starts in May and ends in July and I am on the June 2014 section, that event won’t show up.

    I’m looking for a function/query – combo that will let me first filter by the month I am in, but be flexible enough to grab those events that span multiple months.

    I’ve tried a few things, which I will list below. I’ve tested all of these queries and none of them seem to be able to treat my dates like a true date range.

    It’s suggested that maybe i need an additional field that I can update when the post is submitted to tell wordpress which months to show the event in. I would query that field instead of my date fields. I’m trying to avoid doing that because I’d like to make this system more legit.

    Let me know if you all have some suggestions for me on what i can do.

    Here is what i have already tried (if needed i can provide a link to the website via PM):

    $all_events = array (
    'post_type' => 'events',
        'posts_per_page' => 50,
        'meta_query' => array(
        array(
            'key'       => 'event_start_date',
            'compare'   => '>=',
            'value'     => $startday,
        ),
         array(
            'key'       => 'event_end_date',
            'compare'   => '<=',
            'value'     => $endday,
        )
        ),
    );

    The above query almost works. It will display the events if the start and end date are within the same month, but I don’t see the events that end in another month or two months from now.

    $querystr = "
        SELECT *
        FROM $wpdb->posts wposts, $wpdb->postmeta metastart, $wpdb->postmeta metaend
        WHERE (wposts.ID = metastart.post_id AND wposts.ID = metaend.post_id)
            AND (metastart.meta_key = 'event_start_date' AND metastart.meta_value > $startday )
            AND (metaend.meta_key = 'event_end_date' AND metaend.meta_value <= $endday )
            AND wposts.post_type = 'events'
        AND wposts.post_status = 'publish'
        ORDER BY metastart.meta_value ASC
     ";

    The above query does the same thing as the previous one. What do you think I am missing here?

    NOTE: This was related to this post I made a few months ago. Now getting around to replacing the functionality.

    http://ww.wp.xz.cn/support/topic/upgrading-to-wordpress-and-the-events-calendar-break-my-monthyear-filter?replies=3

Viewing 11 replies - 16 through 26 (of 26 total)
  • Hi Robbiegod,

    Thanks a lot, would be nice to see how thats done.

    A happy (late) thanksgiving to you to!

    Thread Starter robbiegod

    (@robbiegod)

    Sorry about the long delay in replying. I had totally forgotten about your question. So to maybe help guide you through what i did here are some tips.

    First thing i did was i made page called Events Widget (you can call it whatever you want.) I made a blank WordPress Template and removed the header and footer functions because the query inside of the page is just going to output a list of events and I’ll load it in later with ajax. I have a default query where i build the start date as being “today” and then the end date is this month, this year, and the last day of this month. That’s the way I get all of events that happen in this month.

    Then i applied the function we spoke about above to modify the query and output all of the events in the query that fit in the date range.

    Later I modified this same page by allowing the query strings to define the startday and endday. If _m and _y are defined then they are processed and the start day and end day are created based on the query string. So that’s how i am changing the query.

    Then on the homepage I have an area where i am loading the events and i display them by month / year. There are two arrows to flip through the events by month.

    Here is all of that code:

    <?php
    global $post;
    if (isset($_GET['_m'])) {
    	$current_month = date('m', $_GET['_m']);
    	$current_year = $_GET['_y'];
    } else {
    	$current_month = date('m'); // This gets the current month
    	$current_year = date('Y');
    }
    ?>
    <script language="javascript">var mnth=<?php echo $current_month; ?>;var yr=<?php echo $current_year; ?>;</script>
    <p><strong><a id="events-nav-prev" href="#prev"><<</a> | <a id="events-nav-next" href="#next">>></a></strong> <span class="date-info"></span></p>
    <img src="<?php bloginfo('template_url'); ?>/images/loading.gif" alt="Loading..." id="upcoming-events-loader" style="visibility:hidden;" />
    </div>
    
    <div id="upcoming-events-scroller" class="scrollable">
    <script language="javascript" type="text/javascript">
    /* use a function for the exact format desired... */
    var d = new Date();
    var m_names = new Array("January", "February", "March","April", "May", "June", "July", "August", "September", "October", "November", "December");
    
    function getEvents(getAll) {
    	jQuery('#upcoming-events-loader').css('visibility', 'visible');
    	jQuery('#upcoming-events-months .date-info').html(m_names[mnth-1] + " " + yr);
    	jQuery.get('/events-widget/', {
    			_m:mnth,
    			_y:yr,
    			getAll:getAll
    		}, function(data) {
    			jQuery('#upcoming-events-scroller').html(data);
    			jQuery('#upcoming-events-loader').css('visibility', 'hidden');
    		});
    	}
    
    jQuery(document).ready(function() {
    	getEvents('true');
    
    	// bind nav events
    	jQuery('#events-nav-prev').click(function() {
    		if (mnth == 1) {
    			mnth = 12;
    			yr -= 1;
    		} else {
    				mnth -= 1;
    		}
    		console.log(mnth);
    
    	jQuery('#upcoming-events-months .date-info').html(m_names[mnth-1] + " " + yr);
    			getEvents();
    			return false;
    		});
    
    	jQuery('#events-nav-next').click(function() {
    			if (mnth == 12) {
    				mnth = 1;
    				yr += 1;
    			} else {
    				mnth += 1;
    			}
    			console.log(mnth);
    
    	jQuery('#upcoming-events-months .date-info').html(m_names[mnth+1] + " " + yr);
    			getEvents();
    			return false;
    	});
    });
    </script>
    Thread Starter robbiegod

    (@robbiegod)

    I just updated WordPress to version 4.2.1. I also update ACF to v5.2.3 PRO. Sadly, now my filter for dates no longer works.

    I’m only getting events that are in the current month only. So if an event spans from April to May, that even won’t appear. Only events that have a start date and end date in May will appear.

    If someone has a suggested to update the filter to make it work again, I’d appreciate the assistance.

    I’m on the hunt now for a fix.

    Moderator keesiemeijer

    (@keesiemeijer)

    Hi robbiegod

    Try it with this plugin (click the “Download ZIP button).
    https://github.com/keesiemeijer/meta-date-archive

    To have it use your meta key names you have to put this in your (child) theme’s functions.php file.

    add_filter( 'meta_date_archive_start', 'meta_date_archive_start_key' );
    add_filter( 'meta_date_archive_end',   'meta_date_archive_end_key' );
    
    function meta_date_archive_start_key( $key ) {
    	// Return your event start key
    	return 'event_start_date';
    }
    
    function meta_date_archive_end_key( $key ) {
    	// Return your event end key
    	return 'event_end_date';
    }

    Do a query like this (without adding and removing the ‘get_meta_sql’ filter):

    <?php
    $all_events = array (
    	'post_type'               => 'events',
    	'posts_per_page'          => 50,
    	'meta_archive_start_date' => '20140601',
    	'meta_archive_end_date'   => '20140630',
    );
    
    $date_query = new WP_Query( $all_events );
    ?>

    I did not test it with WordPress 4.2.1 but it should work.

    Thread Starter robbiegod

    (@robbiegod)

    so I add all of that in addition to the code I already have. I’ll try this out tomorrow. I hope it works.

    Moderator keesiemeijer

    (@keesiemeijer)

    Remove the WP_Query() and the add_filter() and remove_filter() functions you have now. Activate the plugin and use a similar query in your theme template files as in my last post.

    Thread Starter robbiegod

    (@robbiegod)

    thanks keesiemeijer! This worked perfectly!

    Moderator keesiemeijer

    (@keesiemeijer)

    Hi robbiegod

    I forgot to mention the plugin (you have now) will replace your date archives with the posts with the start and end date custom fields.

    I’ve updated the plugin to not have the date archives replaced by default.

    Can you replace the archive plugin you have now with the updated version.
    https://github.com/keesiemeijer/meta-date-archive

    Thread Starter robbiegod

    (@robbiegod)

    sure, i had just done a round of testing with the previous version and it all seemed to work great. I’ll deploy this to my dev site and then to my live site.

    Thread Starter robbiegod

    (@robbiegod)

    just deployed it to my development website and no issues to report, it still works! handy plugin. I was originally hoping to be able to do this query without a plugin, but your plugin is light enough that i think its a good solution. Awesome to be able to replace a whole Events Calendar plugin with this one light plugin. I do wish the ACF date picker fields would be able to work this way out of the box.

    Anyway, thanks for your excellent assistance. Great plugin!

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome. Thanks for the feedback πŸ™‚

Viewing 11 replies - 16 through 26 (of 26 total)

The topic ‘Query custom post types by two custom date fields’ is closed to new replies.