0

I have written code to get certain tags, and then show the products that have those tags.

This is the code:

$tag_string = '';

for ($i = 0; $i < 8; $i++) {
    for ($j = 0; $j < 8; $j++) {

        $tag_to_use = get_post_meta($post->ID, '_tag_to_use_'.$i.'_'.$j, true);
        $tag_string = $tag_to_use ? $tag_string .= $tag_to_use . ',' : $tag_string .= '';

    }
}

if ( substr( $tag_string, -1) == ',' ) {
    $tag_string = rtrim( $tag_string, ',' );
}

echo do_shortcode( '[products tag="' . $tag_string . '"]' );

}

When I run th code, I get slow page loads.

When I change this:

echo do_shortcode( '[products tag="' . $tag_string . '"]' );

To this:

echo do_shortcode( '[products tag="test,test1"]' );

The page loads fast.

Why is that happening, and how can I fix it? If any more information is needed, tell me and I'll update the question.

Thanks!

  • What is the output of var_dump( $tag_string ); ? And instead of calling do_shortcode(), you could just directly call the function which handles the products shortcode. – Sally CJ Mar 19 at 7:40
  • This is the output of var_dump( $tag_string ); - string(22) "test,test1,test2,Aztec" . What is the function it calls? – Omer Mar 19 at 7:45
  • And is it still slow with echo do_shortcode( '[products tag="test,test1,test2,Aztec"]' ) ? – Sally CJ Mar 19 at 7:52
  • Yes. I have changed the shortcode to this: [products limit="8" paginate="true" tag="' . $tag_string . '"] and now it loads fine. Do you know how I could change it to infinte scroll instead of pagination? – Omer Mar 19 at 7:56
  • It's not that I don't know, but try searching for an existing question/answer/solution - or a plugin like this.. – Sally CJ Mar 19 at 8:12
1

I'm not sure, if it is the root of the problem, but the thing that I noticed is that your code ends up calling get_post_meta quite many times (64? 8 loops within 8 loops, yes?) for the same post.

Perhaps you could get all of the post meta just once, before the for loop. Then within your loop use isset() or ! empty() to check if the data exists.

Something along these lines,

$post_meta = get_post_meta($post->ID); // This returns an array with all the data in position 0, if I remember correctly
for ($i = 0; $i < 8; $i++) {
  for ($j = 0; $j < 8; $j++) {
    if ( isset( $post_meta[0]['_tag_to_use_'.$i.'_'.$j] ) ) {
      $tag_to_use = $post_meta[0]['_tag_to_use_'.$i.'_'.$j];
      // The meta value might be in a array with the position 0, var_dump $tag_to_use to see if this is the case
      // $tag_to_use = $post_meta[0]['_tag_to_use_'.$i.'_'.$j][0];
      $tag_string = $tag_to_use ? $tag_string .= $tag_to_use . ',' : $tag_string .= '';
    }
  }
}

Use var_dump to check that you get the array indexes correct and maybe_unserialize() the meta data, if needed.

I don't know if this is just broscience and wishful thinking, but maybe it's worth the try. You can of course use microtime to get a rough idea, if the loop above is faster than the original.

  • uh, what's "broscience" ? – SherylHohman Mar 20 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.