0

I have created a custom post type on my website that has categories, and I need to use those categories to create a sort function (by category) on a page that will display certain category posts from that set of post types. However, the categories are not populating correctly. Code is below:

<?php
/**
 * The template for displaying archive pages.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package ODExtranet
 */

get_header(); ?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">
        <header class="page-header">
                <h1><?php echo str_replace("Archives: ", "", get_the_archive_title()); ?></h1>
            </header><!-- .page-header -->                  
                        <?php

                            $sopscats = wp_get_object_terms( get_the_ID(), 'sops-group' );

                            $sopscatids = array();
                            echo $sopscats;

                            if( count( $sopscats ) > 0 ) {

                                foreach ( $sopscats as $sopscat) {
                                    $sopscatids[] = $sopscat->term_id;
                                }

                            }

                            if( $sopscats == false OR count( $sopscats ) > 1 ) {

                                echo '<ul id="portfolio-filter" class="bottommargin clearfix"><li class="activeFilter"><a href="#" data-filter="all">' . __('All', 'coworker') . '</a></li>';

                                if( count( $sopscatids ) > 0 ) {
                                    $terms = get_terms( "sops-group", array( 'include' => $sopscatids ) );
                                } else {
                                    $terms = get_terms( "sops-group" );
                                }

                                $count = count( $terms );
                                if ( $count > 0 ){
                                    foreach ( $terms as $term ) {
                                        echo '<li><a href="#" data-filter=".sop-' . $term->slug . '">' . $term->name . '</a></li>';
                                    }
                                }

                                echo '</ul>';

                            }

                        ?>


                        <?php

                        $sopscount = is_numeric( get_post_meta( get_the_ID(), 'semi_page_sops_items', TRUE ) ) ? get_post_meta( get_the_ID(), 'semi_page_sops_items', TRUE ) : -1;

                        $sopsorder = get_post_meta( get_the_ID(), 'semi_page_sops_order', TRUE );

                        $sopsorderby = get_post_meta( get_the_ID(), 'semi_page_sops_orderby', TRUE );

                        $args = array( 'post_type' => 'sops', 'posts_per_page' => $sopscount, 'order' => $sopsorder, 'orderby' => $sopsorderby );

                        if( count( $sopscatids ) > 0 ) {
                            $args['tax_query'] = array( array( 'taxonomy' => 'sops-group', 'field' => 'id', 'terms' => $sopscatids ) );
                        }

                        $sops = new WP_Query( $args );

                        if( $sops->have_posts() ):

                        ?>

                        <div id="sops" class="clearfix">

                            <?php while ( $sops->have_posts() ) : $sops->the_post();

                            $getterms = get_the_terms( get_the_ID(), 'sops-group' );

                            if ( $getterms ) {

                                $sopterms = array();

                                foreach ($getterms as $getterm) {
                                    $sopterms[] = 'sop-' . $getterm->slug;
                                }

                                $sopterms = implode( " ", $sopterms );

                            }

                            ?>

                            <div class="toggle sop <?php echo $sopterms; ?> clearfix">

                                <div class="togglet"><i class="<?php echo get_post_meta( get_the_ID(), 'semi_sop_icon', TRUE ); ?>"></i><?php the_title_attribute(); ?></div>

                                <div class="togglec clearfix">

                                    <?php echo get_the_content(); ?>

                                </div>

                            </div>

                            <?php endwhile; ?>

                        </div>

                        <script type="text/javascript">

                            jQuery(document).ready(function($){

                                var $sopItems = $('#sops .sop');

                                $('#portfolio-filter a').click(function(){

                                    $('#portfolio-filter li').removeClass('activeFilter');

                                    $(this).parent('li').addClass('activeFilter');

                                    var sopSelector = $(this).attr('data-filter');

                                    $sopItems.css('display', 'none');

                                    if( sopSelector != 'all' ) {

                                        $( sopSelector ).fadeIn(500);

                                    } else {

                                        $sopItems.fadeIn(500);

                                    }

                                    return false;

                               });

                            });

                        </script>

                        <?php endif; wp_reset_postdata(); ?>


                    </div>


                    <?php get_sidebar(); ?>

        </main><!-- #main -->
    </div><!-- #primary -->

<?php
get_sidebar();
get_footer();

When I try to debug, it seems that $sopscats is not being populated (echo $sopscats; is showing "Array".

print_r( $sopscats ) shows: Array ( [0] => WP_Term Object ( [term_id] => 20 [name] => Cat 1 [slug] => cat-1 [term_group] => 0 [term_taxonomy_id] => 20 [taxonomy] => sops-group [description] => [parent] => 0 [count] => 1 [filter] => raw ) )

I see that count is greater than 0, but is only showing one, even though I have 3 categories created and set on 3 different posts.

EDIT: I have added a second category to the first post, and the filters are now showing and working properly. Seems as though the code is looking only at the first post in the set of post types, rather than at the entire set of posts.

  • 1
    wp_get_object_terms returns an array, and you can't echo an array, so seeing "Array" is the expected result. Try print_r( $sopscats ) to see what it contains. – Milo Mar 4 '17 at 16:32
  • print_r shows me this: Array ( [0] => WP_Term Object ( [term_id] => 20 [name] => Cat 1 [slug] => cat-1 [term_group] => 0 [term_taxonomy_id] => 20 [taxonomy] => sops-group [description] => [parent] => 0 [count] => 1 [filter] => raw ) ) – Trenton Moore Mar 4 '17 at 16:36
  • you check if there is more than one term with count( $sopscats ) > 1. since there is only one, it doesn't pass this test. – Milo Mar 4 '17 at 16:46
  • I managed to sort this out somehow. Basically just got rid of the if > 0 and if > 1 statements and have it set to show the filter at all times. – Trenton Moore Mar 13 '17 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.