• Resolved simonebiffi

    (@simonebiffi)


    Hi there, I was wondering if someone knows how I can optimize, and hopefully dramatically shrink the function below.

    It is used within the function.php of a custom template and it’s used to retrieved custom designs for each of the individual sections of the blog/site. The problem is that it’s immensely long and it seems to cause some little CPU overload on the server.

    Anybody would know how to optimize it? Thanks in advance!

    /********************************************************************
    loading a custom single post template
    ********************************************************************/
    function get_custom_post() {
    
    	global $post, $chic;
    	$get_post_cat = wp_get_post_categories($post->ID);
    	foreach($get_post_cat as $catid ) {
    	$cat = get_category($catid);
    	$the_cat = get_cat_name($cat->cat_ID);
    	}
    
    	// videos
    	$video_sustainability = get_cat_name($chic['video_sustainability_id']);
    	$video_art_and_design = get_cat_name($chic['video_art_design_id']);
    	$video_conscious_living = get_cat_name($chic['video_conscious_living_id']);
    	$video_wellbeing = get_cat_name($chic['video_wellbeing_id']);
    	// Wellbeing
    	$in_the_balance = get_cat_name($chic['in_the_balance_id']);
    	$intimacy = get_cat_name($chic['intimacy_id']);
    	$social_trends = get_cat_name($chic['social_trends_id']);
    	$sustainable_life = get_cat_name($chic['sustainable_life_id']);
    	$wellness_notes = get_cat_name($chic['wellness_notes_id']);
    	// Travel
    	$dream_destinations = get_cat_name($chic['dream_destinations_id']);
    	$globe_trotter = get_cat_name($chic['globe_trotter_id']);
    	$haute_hotels = get_cat_name($chic['haute_hotels_id']);
    	$spas = get_cat_name($chic['spas_id']);
    	$wanderlust = get_cat_name($chic['wanderlust_id']);
    	// Culture
    	$art_and_design = get_cat_name($chic['art_and_design_id']);
    	$books = get_cat_name($chic['books_id']);
    	$cuisine = get_cat_name($chic['cuisine_id']);
    	$on_the_agenda = get_cat_name($chic['on_the_agenda_id']);
    	$photography = get_cat_name($chic['photography_id']);
    	$lifestyle_notes = get_cat_name($chic['lifestyle_notes_id']);
    	// Fashion
    	$style_mantra = get_cat_name($chic['style_mantra_id']);
    	$style_notes = get_cat_name($chic['style_notes_id']);
    	$street_style = get_cat_name($chic['street_style_id']);
    	$trendwatch = get_cat_name($chic['trendwatch_id']);
    	$stylist_on_demand = get_cat_name($chic['stylist_on_demand_id']);
    	$wish_list = get_cat_name($chic['wish_list_id']);
    	// Beauty
    	$must_have_products = get_cat_name($chic['must_have_products_id']);
    	$beauty_trends = get_cat_name($chic['beauty_trends_id']);
    	$dream_destinations = get_cat_name($chic['dream_destinations_id']);
    	$scentsual = get_cat_name($chic['scentsual_id']);
    	$top_tips = get_cat_name($chic['top_tips_id']);
    	$beaute_notes = get_cat_name($chic['beaute_notes_id']);
    	// Celebs
    	$interviews = get_cat_name($chic['interviews_id']);
    	$chicest_celebs = get_cat_name($chic['chicest_celebs_id']);
    	$get_the_look = get_cat_name($chic['get_the_look_id']);
    	$the_good_celebritan = get_cat_name($chic['the_good_celebritan_id']);
    	$the_red_carpet = get_cat_name($chic['the_red_carpet_id']);
    	$celebrity_notes = get_cat_name($chic['celebrity_notes_id']);
    	// Magazine
    	$back_issues = get_cat_name($chic['back_issues_id']);
    	$current_issue = get_cat_name($chic['current_issue_id']);
    	// Competitions
    	$current_competitions = get_cat_name($chic['current_competitions_id']);
    	$past_competitions = get_cat_name($chic['past_competitions_id']);
    
    	// re-set variable for-each
    	// Videos
    	$article_video_sustainability = '/^'. $video_sustainability .'/';
    	$article_video_art_and_design = '/^'. $video_art_and_design .'/';
    	$article_conscious_living = '/^'. $video_conscious_living .'/';
    	$article_video_wellbeing = '/^'. $video_wellbeing .'/';
    	// Wellbeing
    	$article_in_the_balance = '/^'. $in_the_balance .'/';
    	$article_intimacy = '/^'. $intimacy .'/';
    	$article_social_trends = '/^'. $social_trends .'/';
    	$article_sustainable_life = '/^'. $sustainable_life .'/';
    	$article_wellness_notes = '/^'. $wellness_notes .'/';
    	// Travel
    	$gallery_dream_destinations = '/^'. $dream_destinations .'/';
    	$blog_globe_trotter = '/^'. $globe_trotter .'/';
    	$gallery_haute_hotels = '/^'. $haute_hotels .'/';
    	$gallery_spas = '/^'. $spas .'/';
    	$article_wanderlust = '/^'. $wanderlust .'/';
    	// Culture
    	$gallery_art_and_design = '/^'. $art_and_design .'/';
    	$article_books = '/^'. $books .'/';
    	$article_cuisine = '/^'. $cuisine .'/';
    	$article_on_the_agenda = '/^'. $on_the_agenda .'/';
    	$gallery_photography = '/^'. $photography .'/';
    	$article_lifestyle_notes = '/^'. $lifestyle_notes .'/';
    	// Fashion
    	$gallery_style_mantra = '/^'. $style_mantra .'/';
    	$article_style_notes = '/^'. $style_notes .'/';
    	$blog_street_style = '/^'. $street_style .'/';
    	$gallery_trendwatch = '/^'. $trendwatch .'/';
    	$gallery_stylist_on_demand = '/^'. $stylist_on_demand .'/';
    	$gallery_wish_list = '/^'. $wish_list .'/';
    	// Beauty
    	$gallery_must_have_products = '/^'. $must_have_products .'/';
    	$gallery_beauty_trends = '/^'. $beauty_trends .'/';
    	$gallery_scentsual = '/^'. $scentsual .'/';
    	$gallery_top_tips = '/^'. $top_tips .'/';
    	$article_beaute_notes = '/^'. $beaute_notes .'/';
    	// Celebs
    	$article_interviews = '/^'. $interviews .'/';
    	$gallery_chicest_celebs = '/^'. $chicest_celebs .'/';
    	$gallery_get_the_look = '/^'. $get_the_look .'/';
    	$article_the_good_celebritan = '/^'. $the_good_celebritan .'/';
    	$gallery_the_red_carpet = '/^'. $the_red_carpet .'/';
    	$article_celebrity_notes = '/^'. $celebrity_notes .'/';
    	// Magazine
    	$article_back_issues = '/^'. $back_issues .'/';
    	$article_current_issue = '/^'. $current_issue .'/';
    	// Competitions
    	$article_current_competitions = '/^'. $current_competitions .'/';
    	$article_past_competitions = '/^'. $past_competitions .'/';
    
    	// IF condition Videos
    	if (preg_match($article_video_sustainability, $the_cat)) {
    		include TEMPLATEPATH. '/article_video_sustainability.php';
    	} elseif (preg_match($article_video_art_and_design, $the_cat)) {
    		include TEMPLATEPATH. '/article_video_art_design.php';
    	} elseif (preg_match($article_conscious_living, $the_cat)) {
    		include TEMPLATEPATH. '/article_video_conscious_living.php';
    	} elseif (preg_match($article_video_wellbeing, $the_cat)) {
    		include TEMPLATEPATH. '/article_video_wellbeing.php';
    	} 
    
    	// Wellbeing
    	elseif (preg_match($article_in_the_balance, $the_cat)) {
    		include TEMPLATEPATH. '/article_in_the_balance.php';
    	} elseif (preg_match($article_intimacy, $the_cat)) {
    		include TEMPLATEPATH. '/article_intimacy.php';
    	} elseif (preg_match($article_social_trends, $the_cat)) {
    		include TEMPLATEPATH. '/article_social_trends.php';
    	} elseif (preg_match($article_sustainable_life, $the_cat)) {
    		include TEMPLATEPATH. '/article_sustainable_life.php';
    	} elseif (preg_match($article_wellness_notes, $the_cat)) {
    	    include TEMPLATEPATH. '/article_wellness_notes.php';
    	} 
    
    	// Travel
    	elseif (preg_match($gallery_dream_destinations, $the_cat)) {
    		include TEMPLATEPATH. '/gallery_dream_destinations.php';
    	} elseif (preg_match($blog_globe_trotter, $the_cat)) {
    		include TEMPLATEPATH. '/blog_globe_trotter.php';
    	} elseif (preg_match($gallery_haute_hotels, $the_cat)) {
    		include TEMPLATEPATH. '/gallery_haute_hotels.php';
    	} elseif (preg_match($gallery_spas, $the_cat)) {
    		include TEMPLATEPATH. '/gallery_spas.php';
    	} elseif (preg_match($article_wanderlust, $the_cat)) {
    	    include TEMPLATEPATH. '/article_wanderlust.php';
    	} 
    
    	// Culture
    	elseif (preg_match($gallery_art_and_design, $the_cat)) {
    		include TEMPLATEPATH. '/gallery_art_and_design.php';
    	} elseif (preg_match($article_books, $the_cat)) {
    		include TEMPLATEPATH. '/article_books.php';
    	} elseif (preg_match($article_cuisine, $the_cat)) {
    		include TEMPLATEPATH. '/article_cuisine.php';
    	} elseif (preg_match($article_on_the_agenda, $the_cat)) {
    		include TEMPLATEPATH. '/article_on_the_agenda.php';
    	} elseif (preg_match($gallery_photography, $the_cat)) {
    		include TEMPLATEPATH. '/gallery_photography.php';
    	} elseif (preg_match($article_lifestyle_notes, $the_cat)) {
    	    include TEMPLATEPATH. '/article_lifestyle_notes.php';
    	} 
    
    	// Fashion
    	elseif (preg_match($gallery_style_mantra, $the_cat)) {
    		include TEMPLATEPATH. '/gallery_style_mantra.php';
    	} elseif (preg_match($article_style_notes, $the_cat)) {
    		include TEMPLATEPATH. '/article_style_notes.php';
    	} elseif (preg_match($blog_street_style, $the_cat)) {
    		include TEMPLATEPATH. '/blog_street_style.php';
    	} elseif (preg_match($gallery_trendwatch, $the_cat)) {
    		include TEMPLATEPATH. '/gallery_trendwatch.php';
    	} elseif (preg_match($gallery_stylist_on_demand, $the_cat)) {
    		include TEMPLATEPATH. '/gallery_stylist_on_demand.php';
    	} elseif (preg_match($gallery_wish_list, $the_cat)) {
    		include TEMPLATEPATH. '/gallery_wish_list.php';
    	} 
    
    	// Beauty
    	elseif (preg_match($gallery_must_have_products, $the_cat)) {
    		include TEMPLATEPATH. '/gallery_must_have_products.php';
    	} elseif (preg_match($gallery_beauty_trends, $the_cat)) {
    		include TEMPLATEPATH. '/gallery_beauty_trends.php';
    	} elseif (preg_match($gallery_scentsual, $the_cat)) {
    		include TEMPLATEPATH. '/gallery_scentsual.php';
    	} elseif (preg_match($gallery_top_tips, $the_cat)) {
    		include TEMPLATEPATH. '/gallery_top_tips.php';
    	} elseif (preg_match($article_beaute_notes, $the_cat)) {
    	    include TEMPLATEPATH. '/article_beaute_notes.php';
    	} 
    
    	// Celebs
    	elseif (preg_match($article_interviews, $the_cat)) {
    		include TEMPLATEPATH. '/article_interviews.php';
    	} elseif (preg_match($gallery_chicest_celebs, $the_cat)) {
    		include TEMPLATEPATH. '/gallery_chichest_celebs.php';
    	} elseif (preg_match($gallery_get_the_look, $the_cat)) {
    		include TEMPLATEPATH. '/gallery_get_the_look.php';
    	} elseif (preg_match($article_the_good_celebritan, $the_cat)) {
    		include TEMPLATEPATH. '/article_the_good_celebritan.php';
    	} elseif (preg_match($gallery_the_red_carpet, $the_cat)) {
    		include TEMPLATEPATH. '/gallery_the_red_carpet.php';
    	} elseif (preg_match($article_celebrity_notes, $the_cat)) {
    	    include TEMPLATEPATH. '/article_celebrity_notes.php';
    	} 
    
    	// Magazine
    	elseif (preg_match($article_back_issues, $the_cat)) {
    		include TEMPLATEPATH. '/article_magazine.php';
    	} elseif (preg_match($article_current_issue, $the_cat)) {
    		include TEMPLATEPATH. '/article_magazine.php';
    	}
    
    	// Competitions
    	elseif (preg_match($article_current_competitions, $the_cat)) {
    		include TEMPLATEPATH. '/article_competitions.php';
    	} elseif (preg_match($article_past_competitions, $the_cat)) {
    		include TEMPLATEPATH. '/article_competitions.php';
    	} 
    
    	// Conclude
    	else {
    		include TEMPLATEPATH. '/article_general.php';
    	}
    
    }
Viewing 5 replies - 16 through 20 (of 20 total)
  • Thread Starter simonebiffi

    (@simonebiffi)

    Thanks a mille for the tip; in fact by analyzing the CPU weekend report I was astonished to notice that practically all the categories that were simultaneosly queried at once were all those calling a Java Script to display the header dropdown menu. Those not associated with it were in fact behaving as they always did. I’ve been left now but with the option to remove the script (and consequent dropdowns) from the top menu, thus to understand if this could have been the cause. Will need now to double check the report later today to confirm.

    This apparently the infamous JS:

    <!-- Java Script for Dropdown Menu -->
    <script type="text/javascript">
    function showmenu(elmnt)
    { document.getElementById(elmnt).style.visibility="visible"; }
    function hidemenu(elmnt)
    { document.getElementById(elmnt).style.visibility="hidden"; }
    </script>

    And here the calls in the header:

    <div class="p_wellbeing" onmouseover="showmenu('wellbeing')" onmouseout="hidemen ('wellbeing')" onclick="location.href='https://www.chictoday.com/category/wellbeing/';" >
      <div class="dropdown" id="wellbeing" >
        <div class="dlink"><a href="" class="ddlink">In the Balance</a></div>
        <div class="dlink"><a href="" class="ddlink">Social Trends</a></div>
        <div class="dlink"><a href="" class="ddlink">Intimacy</a></div>
        <div class="dlink"><a href="" class="ddlink">Sustainable Life</a></div>
        </div>
    </div>

    In case you want to see the code, I can do better than posting the page on wordpress.pastebin.ca; if you want I can upload a .zip of the entire theme onto the server for you to download; just let me know and I will post a direct link to it.

    Last but not least, the single function to prevent all the IFs conditions. Forgive my ineptitude on this, but I am confused as per how it would work. From the code above it seems to me that it ought to be created 2 .php files for each individual category? One cattemplate-$catID.php whitin which insert the <?php include function > and the final file which is actually the page that will be ultimately loaded, right (e.g.: article_in_the_balance.php)? If so, I assume I would find myself with quite plenty of files around?

    Anyway, let’s see if the JS is the casue, and then I will surely try to implement the suggested function above.

    Glad you found a probable cause.

    You are correct in thinking you would need two files for each unique ultimate file, but you already have the ultimate files created. So, it would just be a matter of creating the cattemplate files. However, I really don’t think that this would make a giant improvement, but it will be worth a try if nothing else works.

    Thread Starter simonebiffi

    (@simonebiffi)

    OK, so not even the JS seems to be the cause and now it’s getting really frustrating… however, since ‘there are no problems, but only solutions’, let’s see if I can find the right one ??

    I ran a Slow Query Analysis on the database (MySQL 4.XXX) and following is a report that I didn’t get before:

    ### 1 Query
    ### Total time: 2, Average time: 2
    ### Taking 2 seconds to complete
    ### Rows analyzed 10871
    use chictoday_com_-_daily;
    SELECT DISTINCT object_id, post_title FROM wp_term_relationships r, wp_term_taxonomy t, wp_posts p WHERE t.term_id IN (SELECT t.term_id FROM wp_term_relationships r, wp_term_taxonomy t WHERE r.term_taxonomy_id = t.term_taxonomy_id AND t.taxonomy = 'XXX' AND r.object_id = XXX) AND r.term_taxonomy_id = t.term_taxonomy_id AND p.post_status = 'XXX' AND p.ID = r.object_id AND object_id <> XXX AND p.post_type = 'XXX' ORDER BY post_date DESC LIMIT XXX;
    
    use chictoday_com_-_daily;
    SELECT DISTINCT object_id, post_title FROM wp_term_relationships r, wp_term_taxonomy t, wp_posts p WHERE t.term_id IN (SELECT t.term_id FROM wp_term_relationships r, wp_term_taxonomy t WHERE r.term_taxonomy_id = t.term_taxonomy_id AND t.taxonomy = 'category' AND r.object_id = 1524) AND r.term_taxonomy_id = t.term_taxonomy_id AND p.post_status = 'publish' AND p.ID = r.object_id AND object_id <> 1524 AND p.post_type = 'post' ORDER BY post_date DESC LIMIT 5;

    As suspected there is something to do with the Category terms, which produce the CPU overload… unfortunately my knowledge stops here. If anybody knows how to correctly interpret the report above and offer suggestions, I’d be TREMENDOUSLY glad! Thanks in advance!

    Thread Starter simonebiffi

    (@simonebiffi)

    OK, after having tried everything, even installed WP Super Cache to improve performances, but still cannot find a solution in regards of the overload.

    The only thing I’m left to think of is that the ‘Category’ and ‘Post’ query in the database are the culprit. If someone knows how to ‘clean’ them, without me compromising the entire DB, that would be fantastic!

    Thread Starter simonebiffi

    (@simonebiffi)

    OK, since I found mor appropriate to open another more targeted thread on this, I think it’s appropriate to mark this as resolved. Thanks everybody.

Viewing 5 replies - 16 through 20 (of 20 total)
  • The topic ‘How to optimize this function’ is closed to new replies.