WP_Query performance issues
-
Hello,
I created custom post type to store information about car colors and created a search form to be able to search on 3 different criteria : color code, color name or color category. I have about 1000 entries in the DB and the response time is really very slow, even if i make a request where their are only some results.
Below is my code (PHP is new for me) which creates the request and shows the result :
There is probably something to improve to make these requests quicker but didn’t find an answer yet by searching on the we 🙁
Thanks in advance for your help !global $post; /* if(isset($_POST['categorie_couleur']) && !empty($_POST['categorie_couleur'])){ $Col1_Array = $_POST['categorie_couleur']; $pp_categorie_couleur_list = ''; foreach($Col1_Array as $selectValue){ $pp_categorie_couleur_list = $pp_categorie_couleur_list.$selectValue.","; } // pour supprimer la dernière virgule $pp_categorie_couleur=substr($pp_categorie_couleur_list, 0, -1); } */ theme_post_wrapper( array( 'id' => theme_get_post_id(), 'class' => theme_get_post_class(), 'title' => theme_get_meta_option($post->ID, 'theme_show_page_title') ? get_the_title() : '', 'heading' => theme_get_option('theme_single_article_title_tag'), 'before' => theme_get_metadata_icons('edit', 'header'), 'content' => do_shortcode(pp_recherche_options_couleur($_POST['recherche_couleur'],$_POST['type_recherche_couleur']) ) ) ); /* fonction permettant de checker si une URL existe */ function URLIsValid($URL) { $exists = true; $file_headers = @get_headers($URL); $InvalidHeaders = array('404', '403', '500'); foreach($InvalidHeaders as $HeaderVal) { if(strstr($file_headers[0], $HeaderVal)) { $exists = false; break; } } return $exists; } /* * Function pp_recherche_options_couleur * Génère la requête sur base des information spécifiées dans le formulaire et retourne le résultat */ function pp_recherche_options_couleur ($recherche_couleur, $type_recherche_couleur) { //echo "recherche couleur : ".$recherche_couleur."<br/>"; //echo "type recherche couleur : ".$type_recherche_couleur."<br/>"; switch ($type_recherche_couleur) { case "nom": $pp_args= array('post_type' => 'code_couleur_ext', 'post_status' => 'publish', 'posts_per_page' => -1, //'no_found_rows' => true, 'cache_results' => false, 'fields' => 'ids', 'meta_query' => array ( 'relation' =>'OR', array( 'key' => 'pp_color-description_courte', 'value' => $recherche_couleur, 'type' => 'char', 'compare' => 'LIKE' ), array( 'key' => 'pp_color-description_courte_en', 'value' => $recherche_couleur, 'type' => 'char', 'compare' => 'LIKE' ), array( 'key' => 'pp_color-description_courte_de', 'value' => $recherche_couleur, 'type' => 'char', 'compare' => 'LIKE' ) ) ); break; case "code": $pp_args= array('post_type' => 'code_couleur_ext', 'post_status' => 'publish', 'posts_per_page' => -1, //'no_found_rows' => true, 'cache_results' => false, 'fields' => 'ids', 'meta_query' => array ( array( 'key' => 'pp_color-code', 'value' => $recherche_couleur, 'type' => 'char', 'compare' => '=' ) ) ); break; case "categorie": $pp_args= array('post_type' => 'code_couleur_ext', 'post_status' => 'publish', 'posts_per_page' => -1, //'no_found_rows' => true, 'cache_results' => false, 'fields' => 'ids', 'meta_key' => 'pp_color-categorie_couleur', 'meta_value' => $recherche_couleur, 'meta_compare' => '=' ); break; } // Pour debug - affichage des valeurs des paramètres $pp_query = new WP_Query($pp_args); $pp_result = ''; $pp_home_url=home_url(); // Affichage de la requete switch ($type_recherche_couleur) { case "nom": $pp_result =$pp_result."<p class='none'><b>Recherche réalisée</b> : Nom de couleur contenant <b><i>".$recherche_couleur."</b></i></p>"; break; case "code": $pp_result =$pp_result."<p class='none'><b>Recherche réalisée</b> : Code couleur = ".$recherche_couleur."</p>"; break; case "categorie": $pp_result =$pp_result."<p class='none'><b>Recherche réalisée</b> : Catégorie couleur = ".$recherche_couleur."</p>"; break; } //affiche le nombre de résultats trouvés $pp_result = $pp_result."<p class='none'><b>Résultats trouvés</b> : ".$pp_query->found_posts."</p>"; // lien pour retour vers la page de recherche $pp_result = $pp_result."<p><a href='".$pp_url_home."/espace-technique/codes-porsche/codes-peinture/'>Retour sur la page de recherche</a></p>"; if ($pp_query->have_posts()) { while ($pp_query->have_posts()) : $pp_query->the_post(); $pp_couleur_code = get_post_meta(get_the_ID(), 'pp_color-code', true ); $pp_couleur_code_description_courte = get_post_meta(get_the_ID(), 'pp_color-description_courte', true ); $pp_couleur_code_description_courte_en = get_post_meta(get_the_ID(), 'pp_color-description_courte_en', true ); $pp_couleur_code_description_courte_de = get_post_meta(get_the_ID(), 'pp_color-description_courte_de', true ); $pp_couleur_nom_fichier_couleur = get_post_meta(get_the_ID(), 'pp_color-nom_fichier_couleur', true ); $pp_couleur_nom_fichier_voiture = get_post_meta(get_the_ID(), 'pp_color-nom_fichier_voiture', true ); $pp_couleur_notes = get_post_meta(get_the_ID(), 'pp_color-notes', true ); $pp_couleur_url = $pp_home_url."/wp-content/uploads/colors/".$pp_couleur_nom_fichier_couleur; $pp_couleur_car_url = $pp_home_url."/wp-content/uploads/colors/".$pp_couleur_nom_fichier_voiture; $pp_result = $pp_result."<table border='1' width='100%'>"; $pp_result = $pp_result."<tr>"; $pp_result = $pp_result."<td rowspan='4' bgcolor='#646464' style='color:#E0DEDF;text-align:center' width='6%' >".$pp_couleur_code."</td>"; // FR $pp_result = $pp_result."<td width='6%'> <img src='".$pp_home_url."/wp-content/uploads/drapeaux/fr.png'/></td>"; $pp_result = $pp_result."<td width='22%'>".$pp_couleur_code_description_courte."</td>"; if (URLIsValid($pp_couleur_url)){ $pp_result = $pp_result."<td rowspan='3' width='23%'><img src='".$pp_couleur_url."' border='1'/></td>"; } else { $pp_result = $pp_result."<td rowspan='3' width='23%'> </td>"; } if (URLIsValid($pp_couleur_car_url)){ $pp_result = $pp_result."<td rowspan='3' width='43%'><img src='".$pp_couleur_car_url."'/></td>"; } else { $pp_result = $pp_result."<td rowspan='3' width='43%'> </td>"; } $pp_result = $pp_result."</tr>"; // EN $pp_result = $pp_result."<tr>"; $pp_result = $pp_result."<td width='6%'> <img src='".$pp_home_url."/wp-content/uploads/drapeaux/gb.png'/></td>"; $pp_result = $pp_result."<td width='22%'>".$pp_couleur_code_description_courte_en."</td>"; $pp_result = $pp_result."</tr>"; // DE $pp_result = $pp_result."<tr>"; $pp_result = $pp_result."<td width='6%'> <img src='".$pp_home_url."/wp-content/uploads/drapeaux/de.png'/></td>"; $pp_result = $pp_result."<td width='22%'>".$pp_couleur_code_description_courte_de."</td>"; $pp_result = $pp_result."</tr>"; // Modèles concernés $pp_result = $pp_result."<tr>"; $pp_result = $pp_result."<td colspan='4' style='padding:8px'>"; $pp_result = $pp_result."[wpspoiler name='Modèles concernés...' open='false']"; $pp_modeles_concernes = get_field('modeles_concernes',get_the_ID()); //var_dump( $pp_modeles_concernes ); $pp_result = $pp_result."<ul>"; foreach( $pp_modeles_concernes as $pp_modele_concerne) { $pp_result = $pp_result."<li>"; $pp_cat_name = get_cat_name($pp_modele_concerne); $pp_result = $pp_result.$pp_cat_name; $pp_result = $pp_result."</li>"; } $pp_result = $pp_result."</ul>"; // temporaire en attendant d'associer les modèles aux couleurs $pp_result = $pp_result."<p>".$pp_couleur_notes."</p>"; $pp_result = $pp_result."[/wpspoiler]"; $pp_result = $pp_result."</td>"; $pp_result = $pp_result."</tr>"; $pp_result = $pp_result."</table>"; $pp_result = $pp_result."<br/>"; endwhile; $pp_result = $pp_result."<p><a href='".$pp_url_home."/espace-technique/codes-porsche/codes-peinture/'>Retour sur la page de recherche</a></p>"; } else { $pp_result = "<h3>Aucun résultat n'a été trouvé avec les critères spécifiés...</h3>"; $pp_result = $pp_result."<a href='".$pp_url_home."/espace-technique/codes-porsche/codes-peinture/'>Retour sur la page de recherche</a>"; } return ($pp_result); }
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
The topic ‘WP_Query performance issues’ is closed to new replies.