0

For some reason the query in this function returns an empty array. I have been looking at this for far too long now and can't figure out why. Does anybody have an idea? Do i use $wpdb->prepare correctly?

function getComponents($page_id, $currentLanguage) {
       global $wpdb;
       $result = $wpdb->get_results($wpdb->prepare(
          'SELECT id, post_content, post_title, post_excerpt, post_name, m1.meta_value AS `template`, m2.meta_value AS `home_description` ' .
          'FROM ' . $wpdb->posts . ' p ' .
          'JOIN wp_term_relationships r ON p.ID=r.object_id ' .
          'JOIN wp_terms t ON r.term_taxonomy_id=t.term_id ' .
          'WHERE t.slug=%s AND post_parent=%d AND post_type="page" AND post_status="publish"' .
          'ORDER BY menu_order ASC ',
          $currentLanguage, $page_id
       ), OBJECT_K);
       return $result;
    }

1 Answer 1

-1

Try with:

function getComponents($page_id, $currentLanguage) {
       global $wpdb;
       $result = $wpdb->get_results($wpdb->prepare( "
          SELECT id, post_content, post_title, post_excerpt, post_name, m1.meta_value AS 'template', m2.meta_value AS 'home_description'
          FROM {$wpdb->posts} p
          JOIN wp_term_relationships r ON p.ID = r.object_id
          JOIN wp_terms t ON r.term_taxonomy_id = t.term_id
          WHERE t.slug=%s AND post_parent = %d AND post_type='page' AND post_status = 'publish'
          ORDER BY menu_order ASC ",
          $currentLanguage, $page_id ), OBJECT_K );
       return $result;
    }
1
  • Pure code dumps are not recommended. Please explain why this solves the problem, thanks. Commented Oct 23, 2016 at 8:03

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.