Support

Account

Forum Replies Created

  • @hube2 Wow! Thanks for all these examples and explanations!

    Here’s what I have tried on a CPT archive page using pre_get_posts. It works fine for the first page but fails on any subsequent pages (example.com/events/page/2). My guess is that it has to do with my conditional statement failing for any pages other than page 1. Any ideas/suggestions/fixes of my mess?

    I’m trying to get each page of the archive show only events for a specific month, starting with the current month. So page 1 = May 2021, page 2 = June 2021, and so on…

    function custom_events_query( $query ) {
    
        if (  $query->is_main_query() && !is_admin() && is_post_type_archive('event')) {
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $query->set( 'post_type', 'event' );
            $query->set( 'meta_key', 'event_date' );
            $query->set( 'orderby', 'meta_value' );
            $query->set( 'order', 'ASC' );
            $query->set( 'posts_per_page', -1 );
            $query->set( 'paged', $paged );
            if($paged == 1) {
                $datequery = date('Ymd');
                $year = date('Y');
                $month = date('m');
                $meta_query_two = array(
                    'relation' => 'AND',
                    array(
                        'key' => 'event_date',
                        'value' => $datequery,
                        'compare' => '>='
                    ),
                    array(
                        'key' => 'event_date',
                        'value' => $year.''.$month.'31',
                        'compare' => '<='
                    )       
                );
            } else {
                $today = date('Ymd');
                $thisMonth = date('m', strtotime($today));
                $year = date('Y', strtotime($today));
                $month = date('m', strtotime($today.'+'.$paged.' month'));
                $datequery = date('Y'.$month.'01');
                $meta_query_two = array(
                    'relation' => 'AND',
                    array(
                        'key' => 'event_date',
                        'value' => $datequery,
                        'compare' => '>='
                    ),
                    array(
                        'key' => 'event_date',
                        'value' => $year.''.$month.'31',
                        'compare' => '<='
                    )       
                );
            }
            $query->set( 'meta_query', $meta_query_two );
        }
    
        return $query;
    }
    add_filter( 'pre_get_posts', 'custom_events_query' );
  • @hube2 I appreciate any help you can provide and totally understand your position regarding your framework and all the work that has gone into it.

    I have an “event” custom post type with the date picker field included. I’m using the following code in a page template to pull in the current month’s events, which is working fine. But I have no clue how to get this to paginate from month to month. What is needed is for the user to arrive on the page showing the current month’s events, then be able to click through to the next month’s events, and so on, including clicking back to the previous month but not past the current month. My guess is that I need to use WP_Query instead of get_posts, but still not sure how to get that to work.

    Again, any help you can provide is greatly appreciated!

    <?php
            $year = date('Y');
            $month = date('m');
            $posts = get_posts(array(
                'post_type' => 'event',
                'posts_per_page' => -1,
                'order' => 'ASC',
                'orderby' => 'meta_value',
                'meta_key' => 'date',
                'meta_query' => array(
                    'relation' => 'AND',
                    array(
                        'key' => 'date',
                        'value' => $year.''.$month.'01',
                        'compare' => '>=',
                    ),
                    array(
                        'key' => 'date',
                        'value' => $year.''.$month.'31',
                        'compare' => '<=',
                    )       
                )
            ));
            if( $posts ) : foreach( $posts as $p ) :
            $date_string = get_field('date', $p->ID);
            $date = DateTime::createFromFormat('Ymd', $date_string);         
            endforeach;
            ?>
            <div class="small-12 columns text-center">
                <h2><?php echo $date->format('F Y'); ?></h2>
            </div>
            <?php
            foreach( $posts as $p ) :
            $background_color = get_field('background_color', $p->ID);
                if($background_color == 'terracotta') {
                    $bkg = '#AD7A67';
                } elseif($background_color == 'sand') {
                    $bkg = '#C3AD93';
                } elseif($background_color == 'dark-blue') {
                    $bkg = '#2D4B62';
                } elseif($background_color == 'seafoam-green') {
                    $bkg = '#91ACA9';
                }
            $date_string = get_field('date', $p->ID); 
            $date = DateTime::createFromFormat('Ymd', $date_string);
            $name = get_the_title($p->ID);
            $start_time = get_field('start_time', $p->ID);
            $description = get_field('description', $p->ID);
            $event_page_link = get_field('event_page_link', $p->ID);
            $register_link = get_field('register_link', $p->ID);
            ?>
            <div class="large-3 small-12 columns">
                <div class="event section--one__content__event" <?php echo 'style="background-color:'.$bkg.';"'; ?>>
                    <?php
                    if($date) echo '<div class="name section--one__content__event__day">'.$date->format('j').'</div>';
                    if($name) echo '<div class="name section--one__content__event__name">'.$name.'</div>';
                    if($start_time) echo '<div class="name section--one__content__event__time">'.$start_time.'</div>';
                    if($description) echo '<div class="position section--one__content__event__description">'.$description.'</div>';
                    if($event_page_link) echo '<a href="'.$event_page_link.'" target="_blank" class="link section--one__content__event__link">EVENT PAGE</a>'; 
                    if($event_page_link && $register_link) echo ' <span class="link section--one__content__event__link">|</span> ';
                    if($register_link) echo '<a href="'.$register_link.'" target="_blank" class="link section--one__content__event__link">REGISTER</a>';
                    ?>
                </div>
            </div>
            <?php 
            endforeach; 
            
            endif; 
            ?>
    
  • I stumbled on this discussion while looking for a similar solution. @hube2 would you be so kind as to share your already-created framework that doesn’t show past events? That is exactly what I’m looking for and I’m hoping your work can save me some time. Thanks in advance!

  • Disregard my last post. Your code worked, Gummi! I missed one line in my last test. After discovering that I fixed it and tested and everything worked. Thank you SO much again and again! Just made mmy day!

  • Man, I am VERY grateful for your help, Gummi. I understand what you’re saying for the most part and I tried the code you provided, but unfortunately got the same results (no update to the checkboxes when submitted). Here’s a snippet of what was output:

    array(2) { 
         ["tri"]=> array(33) { 
              [1]=> string(1) "1" 
              [2352]=> string(1) "1" 
              [886]=> string(1) "1" 

    Does that shed any light on this for you? Obviously I’m out of my depth…

  • Thanks for your suggestions, Gummi, and for taking the time to respond.

    On the line where you update the field, you have $_POST[‘tri’]. I don’t see any form field named ‘tir’ in your code.

    ‘tri’ is the name for ‘field_598c6559e1b0d’. I’ve tried using only field ID wherever it’s referenced, only field name, and every combination of using both in different places without success.

    I tried adding the hidden field as you suggested but that didn’t change anything.

    I’ll try stripping away the conditional items but I really don’t think that has anything to do with it since all of the conditions are working properly. Still, it’s worth a try since nothing else is making this work!

    Any other ideas?

  • Thanks for the help, John. Unfortunately, that tutorial doesn’t address how I would display the custom field values on the User Profile page. Any ideas for that? I’ve Googled and searched through the forums here for all possible keywords I could think of without success.

  • My bad. I forgot to mention (or realize) I was trying to get this to work on a custom taxonomy page, so I forgot to include get_queried_object();

    Resolved now, thanks to my brain fart. Thanks for jogging my mind, John Huebner!

    In case anyone else needs it, here’s my fixed code.

    $queried_object = get_queried_object();
    $seals = get_field('seals', $queried_object);
    $countSeals = count($seals);
Viewing 8 posts - 1 through 8 (of 8 total)