We help IT Professionals succeed at work.
Troubleshooting Question

Displaying Custom Post Type Posts with custom Taxonomy.

Jon Imms
Jon Imms asked
on
34 Views
Last Modified: 2020-10-05
Hello,  

I am working on a page which will show upcoming and on-demand webinars.   I have created a custom post type called webinar, and a 3 custom category (webinar_category) -  (explore, expand, enhance).

I had the future and past posts working (upcoming or on-demand) but now want to split it up, so only show upcoming webinars with the category explore.  

I tried the below code, but it is still displaying all future posts, regardless of category.  

So, how would i modify the below to show only upcoming posts with the webinar_category of "explore" ?

        <div id="block_5eb3286269ae5" class="wp-block-classic align"> 
            <h3 class="subhead blue" style="text-align: center;"><span class="webinar-type">Upcoming webinars</span></h3> 
        </div> 

<?php
// get today's date
            $today = date( 'Y-m-d' );
 
            // get posts
            $futureposts = get_posts(array(
                'post_type'         => 'webinar',
                'posts_per_page'    => -1,
                'meta_key'          => 'date_and_time',
                'meta_value'        => date('Y-m-d'),
                'orderby'           => 'meta_value',
                'order'             => 'ASC',
                'meta_compare'      => '>=',
                'value'             => $today,
                'type'              => 'DATE',
                'tax_query'         => array(
                    array(
                    
                        'taxonomy'          => 'weninar_category',
                        'terms'             => 'explore',
                    )
                ),
            ));
 
            if( $futureposts ): ?>
 
        <div class="row">
 
            <?php foreach( $futureposts as $post ) : setup_postdata( $post ); ?>

            <div class="col-md-4"> 
                <div id="block_5ed527ab633fb" class="wp-block-classic align block-desktop"> 
                    <div style="height: 575px !important;">      
                        <a class="aug20 card" style="cursor: pointer;"><p></p> 
                            <div class="card-image" style="background-color: #003A57;">
                            <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
                                <img src="<?php echo $url ?>" alt="Featured image of this webinar"/>
                            </div> 
                            <div class="card-text"> 
                                <h3><?php the_title(); ?></h3>
                                <p class="date"><?php echo the_field( 'date_and_time' ); ?> CDT</p>
                                <p class="the-tag"><?php echo wp_strip_all_tags(  get_the_term_list( get_the_ID(), 'webinar-tags', ' ', ' , ', ' ')); ?> </p>
                                <p><?php the_excerpt(); ?></p> 
                            </div> 
                        </a>
                        <a class="aug20 card" style="cursor: pointer;"></a>
                        <p><a class="sept9 plumb-button navy-ghost" href="/resources/webinars/aug202020/">Unlock</a> </p>
                    </div> 
                </div> 
            </div>

                <?php wp_reset_postdata(); ?>
                <?php endforeach; ?>
                    
        </div>
 
        <? else: ?>
 
            <p><?php _e( 'No upcoming webinars are scheduled.' ); ?></p>
        
        <?php endif; ?>
Comment
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Jon ImmsWeb Developer

Author

Commented:
hey Robert,

I get  - array(0) { }
Jon ImmsWeb Developer

Author

Commented:
So my full code is -
        <div id="block_5eb3286269ae5" class="wp-block-classic align"> 
            <h3 class="subhead blue" style="text-align: center;"><span class="webinar-type">Upcoming webinars</span></h3> 
        </div> 

<?php
// get today's date
            $today = date( 'Y-m-d' );
 
            // get posts
            $futureposts = get_posts(array(
                'post_type'         => 'webinar',
                'posts_per_page'    => -1,
                'meta_key'          => 'date_and_time',
                'meta_value'        => date('Y-m-d'),
                'orderby'           => 'meta_value',
                'order'             => 'ASC',
                'meta_compare'      => '>=',
                'value'             => $today,
                'type'              => 'DATE',
                'tax_query'         => array(
                    array(
                    
                        'taxonomy'          => 'weninar_category',
                        'terms'             => 'explore',      
                    )
                ),
            ));
            var_dump( $futureposts );

            if( $futureposts ): ?>
 
        <div class="row">
 
            <?php foreach( $futureposts as $post ) : setup_postdata( $post ); ?>

            <div class="col-md-4"> 
                <div id="block_5ed527ab633fb" class="wp-block-classic align block-desktop"> 
                    <div style="height: 575px !important;">      
                        <a class="aug20 card" style="cursor: pointer;"><p></p> 
                            <div class="card-image" style="background-color: #003A57;">
                            <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
                                <img src="<?php echo $url ?>" alt="Featured image of this webinar"/>
                            </div> 
                            <div class="card-text"> 
                                <h3><?php the_title(); ?></h3>
                                <p class="date"><?php echo the_field( 'date_and_time' ); ?> CDT</p>
                                <p class="the-tag"><?php echo wp_strip_all_tags(  get_the_term_list( get_the_ID(), 'webinar-tags', ' ', ' , ', ' ')); ?> </p>
                                <p><?php the_excerpt(); ?></p> 
                            </div> 
                        </a>
                        <a class="aug20 card" style="cursor: pointer;"></a>
                        <p><a class="sept9 plumb-button navy-ghost" href="/resources/webinars/aug202020/">Unlock</a> </p>
                    </div> 
                </div> 
            </div>

                <?php wp_reset_postdata(); ?>
                <?php endforeach; ?>
                    
        </div>
 
        <? else: ?>
 
            <p><?php _e( 'No upcoming webinars are scheduled.' ); ?></p>
        
        <?php endif; ?>



        <div id="block_5eb3286269ae5" class="wp-block-classic align"> 
            <h3 class="subhead orangey" style="text-align: center;"><span class="webinar-type">On-demand webinars</span></h3> 
        </div> 

<?php
// get today's date
            $today = date( 'Y-m-d' );
 
            // get posts
            $pastposts = get_posts(array(
                'post_type'         => 'webinar',
                'posts_per_page'    => -1,
                'meta_key'          => 'date_and_time',
                'meta_value'        => date('Y-m-d'),
                'orderby'           => 'meta_value',
                'order'             => 'DESC',
                'meta_compare'      => '<',
                'value'             => $today,
                'type'              => 'DATE',
                'tax_query'         => array(
                    array(
                    
                        'taxonomy'          => 'weninar_category',
                        'terms'             => 'explore',      
                    )
                ),
            ));
 
            if( $pastposts ): ?>
 
        <div class="row">
 
            <?php foreach( $pastposts as $post ) : setup_postdata( $post ); ?>

            <div class="col-md-4"> 
                <div id="block_5ed527ab633fb" class="wp-block-classic align block-desktop"> 
                    <div style="height: 575px !important;">      
                        <a class="aug20 card" style="cursor: pointer;"><p></p> 
                            <div class="card-image" style="background-color: #003A57;">
                            <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
                                <img src="<?php echo $url ?>" alt="Featured image of this webinar"/>
                            </div> 
                            <div class="card-text"> 
                                <h3><?php the_title(); ?></h3>
                                <p class="the-tag"><?php echo wp_strip_all_tags(  get_the_term_list( get_the_ID(), 'webinar-tags', ' ', ' , ', ' ')); ?> </p>
                                <p><?php the_excerpt(); ?></p>  
                            </div> 
                        </a>
                        <a class="aug20 card" style="cursor: pointer;"></a>
                        <p><a class="sept9 plumb-button navy-ghost" href="/resources/webinars/aug202020/">Unlock</a> </p>
                    </div> 
                </div> 
            </div>

                <?php wp_reset_postdata(); ?>
                <?php endforeach; ?>
                    
        </div>
 
        <? else: ?>
 
            <p><?php _e( 'No On-demand webinars.' ); ?></p>
        
        <?php endif; ?>

        </div><!-- End Explore -->
 If i take out the tax_query part, then al my posts show up correctly, separated into future and past.   atm i have 1 post that has "explore" as the category, and date set to the future, for testing. 


Jon ImmsWeb Developer

Author

Commented:
if i comment out the tax_query - the var dump give

array(7) { [0]=> object(WP_Post)#3230 (24) { ["ID"]=> int(25891) ["post_author"]=> string(2) "21" ["post_date"]=> string(19) "2020-10-03 13:31:30" ["post_date_gmt"]=> string(19) "2020-10-03 18:31:30" ["post_content"]=> string(0) "" ["post_title"]=> string(6) "test 2" ["post_excerpt"]=> string(15) "something here " ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(6) "test-2" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2020-10-03 18:06:36" ["post_modified_gmt"]=> string(19) "2020-10-03 23:06:36" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(63) "https://buildertrendlocal.local/?post_type=webinar&p=25891" ["menu_order"]=> int(0) ["post_type"]=> string(7) "webinar" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [1]=> object(WP_Post)#3231 (24) { ["ID"]=> int(25892) ["post_author"]=> string(2) "21" ["post_date"]=> string(19) "2020-10-03 18:11:14" ["post_date_gmt"]=> string(19) "2020-10-03 23:11:14" ["post_content"]=> string(0) "" ["post_title"]=> string(6) "test 1" ["post_excerpt"]=> string(15) "something here " ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(8) "test-2-5" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2020-10-03 18:13:40" ["post_modified_gmt"]=> string(19) "2020-10-03 23:13:40" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(63) "https://buildertrendlocal.local/?post_type=webinar&p=25892" ["menu_order"]=> int(0) ["post_type"]=> string(7) "webinar" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [2]=> object(WP_Post)#3232 (24) { ["ID"]=> int(25893) ["post_author"]=> string(2) "21" ["post_date"]=> string(19) "2020-10-03 18:11:20" ["post_date_gmt"]=> string(19) "2020-10-03 23:11:20" ["post_content"]=> string(0) "" ["post_title"]=> string(6) "test 2" ["post_excerpt"]=> string(15) "something here " ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(8) "test-2-4" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2020-10-03 18:13:36" ["post_modified_gmt"]=> string(19) "2020-10-03 23:13:36" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(63) "https://buildertrendlocal.local/?post_type=webinar&p=25893" ["menu_order"]=> int(0) ["post_type"]=> string(7) "webinar" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [3]=> object(WP_Post)#3233 (24) { ["ID"]=> int(25894) ["post_author"]=> string(2) "21" ["post_date"]=> string(19) "2020-10-03 18:11:23" ["post_date_gmt"]=> string(19) "2020-10-03 23:11:23" ["post_content"]=> string(0) "" ["post_title"]=> string(6) "test 3" ["post_excerpt"]=> string(15) "something here " ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(8) "test-2-3" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2020-10-03 18:13:31" ["post_modified_gmt"]=> string(19) "2020-10-03 23:13:31" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(63) "https://buildertrendlocal.local/?post_type=webinar&p=25894" ["menu_order"]=> int(0) ["post_type"]=> string(7) "webinar" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [4]=> object(WP_Post)#3234 (24) { ["ID"]=> int(25895) ["post_author"]=> string(2) "21" ["post_date"]=> string(19) "2020-10-03 18:11:27" ["post_date_gmt"]=> string(19) "2020-10-03 23:11:27" ["post_content"]=> string(0) "" ["post_title"]=> string(6) "test 4" ["post_excerpt"]=> string(15) "something here " ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(8) "test-2-2" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2020-10-03 18:13:25" ["post_modified_gmt"]=> string(19) "2020-10-03 23:13:25" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(63) "https://buildertrendlocal.local/?post_type=webinar&p=25895" ["menu_order"]=> int(0) ["post_type"]=> string(7) "webinar" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [5]=> object(WP_Post)#3235 (24) { ["ID"]=> int(25901) ["post_author"]=> string(2) "21" ["post_date"]=> string(19) "2020-10-03 18:15:20" ["post_date_gmt"]=> string(19) "2020-10-03 23:15:20" ["post_content"]=> string(0) "" ["post_title"]=> string(6) "test 5" ["post_excerpt"]=> string(15) "something here " ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(10) "test-2-2-2" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2020-10-03 18:15:29" ["post_modified_gmt"]=> string(19) "2020-10-03 23:15:29" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(63) "https://buildertrendlocal.local/?post_type=webinar&p=25901" ["menu_order"]=> int(0) ["post_type"]=> string(7) "webinar" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } [6]=> object(WP_Post)#3236 (24) { ["ID"]=> int(25902) ["post_author"]=> string(2) "21" ["post_date"]=> string(19) "2020-10-03 18:15:33" ["post_date_gmt"]=> string(19) "2020-10-03 23:15:33" ["post_content"]=> string(0) "" ["post_title"]=> string(6) "test 6" ["post_excerpt"]=> string(15) "something here " ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(12) "test-2-2-2-2" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2020-10-04 15:26:45" ["post_modified_gmt"]=> string(19) "2020-10-04 20:26:45" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(63) "https://buildertrendlocal.local/?post_type=webinar&p=25902" ["menu_order"]=> int(0) ["post_type"]=> string(7) "webinar" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" } }
Jon ImmsWeb Developer

Author

Commented:
Got it working,   Spotted my stupid spelling mistake - "weninar_category". should be "webinar_category".  Changed that and all works now lol
I believe it is because you are not defining the the meta_field.  I don't have a wordpress db open but it is something like:

'taxonomy'          => 'weninar_category',
   ''field'     => 'term_name',
'terms'             => 'explore',
Awesome!  I love it when it is simple.
Jon ImmsWeb Developer

Author

Commented:
Oh no,  looks like it is not fully working. It's separating them into past/future but not category.   I assigned all 13 posts a category. Only 5 in explore, but it's showing all 13.    How do i make show only those in explore? 

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.