WordPress dashboard, viewing CPT results in 504

I created a custom post type with a custom taxonomy. I have about 1500 posts all of which are assigned to some sort of taxonomy term…some being assigned to 200+ terms.

I noticed that from the dashboard, it takes an extremely long amount of time to view the custom post types, most of the time resulting in a 504 Gateway Time-Out.

Querying these post from the front-end is just fine and I have no issues with that.

It’s my understanding that when the dashboard is trying to view these custom post types, it’s trying to pull all the information assigned to each post, before it shows everything.

I do have this WP install set to be able to use as much memory as needed (memory_limit = -1), as I was researching, some said it may be memory related.

Is there something i can do to increase the performance of this query …maybe not load all the taxonomy term relationships of a post until you’re editing the post?

Errors after increasing WP_MEMORY_LIMIT from 1Gb to 2Gb:

WordPress database error: [MySQL client ran out of memory]
SELECT t.*, tt.*, tr.object_id FROM wp_terms AS t INNER JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN wp_term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ('locations') AND tr.object_id IN

WordPress database error: [Lost connection to MySQL server during query]
SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN

WordPress database error: [MySQL server has gone away]
SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month FROM wp_posts WHERE post_type = 'services' ORDER BY post_date DESC

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The CPT and taxonomy terms are created via plugin. I was able to lessen the query by adding the following to my plugin:

add_action( 'pre_get_posts', 'nwtd_lpfs_custom_admin_query' );
function nwtd_lpfs_custom_admin_query( $query ) {
    if( !is_admin() && !$query->is_main_query() ) {
         return;
     }
     if( is_post_type_archive( 'services' ) ) {
          $query->set('no_found_rows', 1 );
          $query->set('update_post_meta_cache', 0 );
          $query->set('update_post_term_cache', 0 );
     }
}

As of currently, I have 700+ posts tied to 1500+ taxonomy terms and everything is working great. The posts are showing up almost instantaneously.

** I did not write this code. Someone I work with directly, who is not part of the WordPress Answers community, provided the code — maybe it can help someone else, or someone else can possibly expand off it to make it more efficient. **


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments