I'm developing a "single page" WordPress based site that has content in multiple languages (using the plug-in WPML) and I need to retrieve it in a more efficient manner to avoid slow page load time.

An example of the code that I'm currently using -

$query = new WP_Query();
$query->query( array( 'post_type' => 'any', 'orderby' => 'id', 'order' => 'ASC' ) );

while ( $query->have_posts() ) {

    // Gather the content for the first section of the page
    if( $post->ID == 100 ) {
        $firstSection .= get_content();

    // Gather next content section etc...


Essentially, what I'm doing right now is looping through all posts looking for a specific ID, for which I then retrieve content tied to a certain part of the page. As you may understand, this gets quite messy after a while, having to create an if statement for each section content for each language. I have around 20 statements inside the while loop currently and I'm sure there is a better way to retrieve the data. Any ideas?

2 Answers 2


WPML has a global to check for the language, though it might be outside the scope of WPSE because this is a commercial plugin.

It works like:

if (ICL_LANGUAGE_CODE == 'en'){
//do something like a custom query...
// do something else
  • In reply to both of you - Perhaps a setup like this: 1. Check the current language 2. Query only the posts from that language (how?) 3. Loop through the query, now there's no need for "double if statements" (2 languages). Aug 22, 2012 at 13:06
  • 1
    Put the query in the conditional language check, which is the example above..
    – Wyck
    Aug 22, 2012 at 13:49
  • Done. Clueless as of how to fetch all posts that are of a specific language via a query though (and with WPML specifically). Aug 22, 2012 at 14:28
  • 1
    There is a query parameter for this documented on the WPML site, but since this is a commercial plugin we tend to close these questions here on stack, it's best if you ask over on their site.
    – Wyck
    Aug 22, 2012 at 15:57
  • Ah, I understand. I have put up a request at their support but it's not the fastest forum. Thanks for the input! Aug 22, 2012 at 18:21

Have a decent look at wp_query.


you can just filter on your post_id at hand. and get only that post. crazy to go like you are doing :)

$query = new WP_Query( 'p=100' );



More on you question: Using Switch Case will be better than your if statements. You could nest them. first switch case your language, then loop your pages.

  • Hm, not sure I understand - why would I want to make a query for every post when I can query them all and loop that query one single time? Aug 22, 2012 at 10:18
  • then I do not quite understand your question I think.
    – woony
    Aug 22, 2012 at 11:02
  • I have a number of posts that I all need to retrieve because the site is a "single page" solution. I figure I can make a query that selects all, then loop through that query and assign a particular ID's content to a variable, which I print in between my HTML (in the example above, $firstSection). Now I manage to solve this, but I'd like to find out if there is a more efficient way of doing this than involving a severe amount of if statements. Aug 22, 2012 at 11:15
  • using a switch might be a bit more efficient. php.net/manual/en/control-structures.switch.php also stackoverflow.com/questions/427760/…
    – woony
    Aug 22, 2012 at 11:20
  • Yes, that might improve the script a bit. I'm more considered how to first check for language, then for the post ID. Not needing to do a language check every time I make a statement, if possible. That would cut down the size significantly. Aug 22, 2012 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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