0

I got HTTP ERROR 500 when I tried to get all the posts from a hierarchical taxonomy which is category. HTTP ERROR 500

I have a page and in this page I want to display all the posts. This page and those posts have same category which called ex. hello.

In the page I used this query below to output the posts from the category which this page have.

<div class="box">
                            <?php
                                $category = get_the_category();
                                if($category->category_parent > 0){
                                    echo $category[0]->cat_name;
                                }

                                $args = array(
                                    'post_type' => 'post',
                                    'tax_query' => array(
                                        'relation' => 'AND',
                                        array(
                                            'taxonomy' => 'category',
                                            'field'    => 'slug',
                                            'terms'    => array( $category[0]->cat_name ),
                                        ),
                                        array(
                                            'taxonomy' => 'post_format',
                                            'field'    => 'slug',
                                            'terms'    => array( 'post-format-video' ),
                                        ),
                                    ),
                                    'order'   => 'ASC',
                                    'nopaging' => true,
                                );

                                $the_query = new WP_Query( $args );

                                while ( $the_query->have_posts() ) {
                                        $the_query->the_post();?>
                                        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><h3><?php the_title(); ?></h3></a>
                                <?php  }

                                // Restore original Post Data
                                wp_reset_postdata();
                            ?>
                        </div>

The query worked fine with the other categories in the other pages which have maximum 300 posts. But category hello have more than 900 posts and when I display theme with the same query I got HTTP ERROR 500.

The category hello is a child of a category called series but this is not the reason of this problem but I wanted just to clarify the issue for you.

Note: When the category hello had 500 posts the page worked and the posts are displaying but the page take more than 3 seconds to load. So the more posts I add to the category the more time the page need to load and in the end if the posts became more the page give me HTTP ERROR 500.

Note: I tried the code on loaclhost and everything worked no errors at all.

Update: I have added this code to wp-config

@ini_set( 'upload_max_filesize' , '99999M' );
@ini_set( 'post_max_size', '99999M');
@ini_set( 'memory_limit', '999999M' );
@ini_set( 'max_execution_time', '300000' );
@ini_set( 'max_input_time', '300000' );

and this code to .htaccess

php_value upload_max_filesize 99999M
php_value post_max_size 99999M
php_value memory_limit 999999M
php_value max_execution_time 300000
php_value max_input_time 300000

I have added these codes above to increase the memory, execution size and upload file size etc.

After I have added them I got 503 ERROR instead of 500 ERROR

  • Generally speaking this more a PHP settings issue than a WordPress one, so you should increase your PHP settings for execution time etc., but there is just so much you can do here. So on the WordPress side of things optimizations should be made, firstly you could consider using something like 'fields' => 'ids', giving you only the ids, but decreasing the load of the query. You have to change your loop accordingly of course. Secondly, when dealing with resource intensive queries, using a caching mechanisms is advisable, e.g. using transients. Lastly, is it really necessary to load all posts. – Nicolai Aug 13 '19 at 9:59
  • Yes I want all the posts because it's like a tv-show with all the episodes – Maher Aldous Aug 13 '19 at 15:42
  • Well, that doesn't mean you have to load them all at once though, you e.g. could divide the loading process. Anyhow, good luck and much success to you. – Nicolai Aug 13 '19 at 16:05
  • Yes I thought about that. I think I will load 100 posts in the first loading then when the page is loaded I will use wordpress ajax to load the other posts. What do you think about that? – Maher Aldous Aug 13 '19 at 17:14
  • It's a good option, don't forget about caching though. – Nicolai Aug 13 '19 at 19:50

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.