• People please help!
    The following code gets all the posts of a specific teacher (teacher is a custom taxonomy) and for each post get the highest category in hierarchy of this post, that is the direct child of a given category. Example: given category is 14. The category hierarchy is: 0->14->32->123, 124, 125 (three categories under 32, each has one post). In this case I get 32 for each post, I push it in the array and then remove identical values. BUT I have a problem in this case: given category is 32. The category hierarchy is the same. In this case I get 123, 124, 125 (direct children of 32). The line that pushes them in the array prevents the page from loading. WHY?!

    $categories_to_display = array(); //declare empty array
                            //get all posts of selected teacher
                            $args = array(
                            'post_type' => 'post',
                            'tax_query' => array(
                                array(
                                'taxonomy' => 'teacher',
                                'field' => 'id',
                                'terms' => $teacher_id
                                 )
                              )
                            );
                            $query = new WP_Query( $args );
                            
                            // The Loop
                            if ( $query->have_posts() ) {
                                while ( $query->have_posts() ) {
                                    $query->the_post();
                                    $categories = get_the_category();
                                    foreach($categories as $cat)
                                    {
                                        $cat_id_to_display = get_nth_parent_category($cat ,$parent_cat_id);
                                        //array_push($categories_to_display, $cat_id_to_display);
                                        $categories_to_display[] = $cat_id_to_display;
                                    }
                                }
                            } 
                            /* Restore original Post Data */
                            wp_reset_postdata();
                            $categories_to_display = array_unique($categories_to_display); //remove duplicate values
                            $categories_to_display = array_filter($categories_to_display); //remove false values

    The function:

    function get_nth_parent_category($cat ,$parent_cat_id_to_stop_at)
    {
        $parent_id = $cat->parent;
        if($parent_id == 0)
        {
            if($parent_cat_id_to_stop_at == 0)
            {
                return $cat->term_id;
            }
            else
            {
                return false;
            }
        }
        
        $parent_cat = get_category($parent_id);
       
         if($parent_cat->term_id == $parent_cat_id_to_stop_at)
            {
                return $cat->term_id;
            }
            else
            {
                return get_nth_parent_category($parent_cat, $parent_cat_id_to_stop_at);
            }
    }
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    It sounds like a PHP error or warning of some sort is happening. Define WP_DEBUG as true on wp-config.php, or check your error logs. Fix the error and the page should then load.

    Unless the assignment occurs earlier, you may have a parent_cat_id is undefined error (in the get_nth_parent_category() call within the foreach loop). If not that, some other error or warning.

Viewing 1 replies (of 1 total)
  • The topic ‘array_push prevents the page from loading’ is closed to new replies.