1

I would be the first to admit that I know next to nothing about SQL queries, but after numerous searches and some trial and error I was able to cobble together a query that returned the results I was looking for.

The problem now is I need to return the results from two taxonomies and I am not sure how to do it. This is my query that returns the results from the first taxonomy:

        $wpdb->get_results(
        "SELECT {$wpdb->prefix}posts.ID, {$wpdb->prefix}posts.post_title, {$wpdb->prefix}posts.post_author, {$wpdb->prefix}posts.post_date, {$wpdb->prefix}terms.name AS 'layout_type'
        FROM
            {$wpdb->prefix}posts
            INNER JOIN {$wpdb->prefix}term_relationships ON ( {$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id )
            INNER JOIN {$wpdb->prefix}terms ON ( {$wpdb->prefix}term_relationships.term_taxonomy_id = {$wpdb->prefix}terms.term_id )
            INNER JOIN {$wpdb->prefix}term_taxonomy ON ( {$wpdb->prefix}term_relationships.term_taxonomy_id = {$wpdb->prefix}term_taxonomy.term_taxonomy_id )
        WHERE
            {$wpdb->prefix}posts.post_type = 'et_pb_layout'
            AND {$wpdb->prefix}term_taxonomy.taxonomy = 'layout_type'
        ORDER BY {$wpdb->prefix}posts.ID DESC",
        ARRAY_A
    );

And here is the query that returns the second one:

        $wpdb->get_results(
        "SELECT {$wpdb->prefix}posts.ID, {$wpdb->prefix}posts.post_title, {$wpdb->prefix}posts.post_author, {$wpdb->prefix}posts.post_date, {$wpdb->prefix}terms.name AS 'scope'
        FROM
            {$wpdb->prefix}posts
            INNER JOIN {$wpdb->prefix}term_relationships ON ( {$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id )
            INNER JOIN {$wpdb->prefix}terms ON ( {$wpdb->prefix}term_relationships.term_taxonomy_id = {$wpdb->prefix}terms.term_id )
            INNER JOIN {$wpdb->prefix}term_taxonomy ON ( {$wpdb->prefix}term_relationships.term_taxonomy_id = {$wpdb->prefix}term_taxonomy.term_taxonomy_id )
        WHERE
            {$wpdb->prefix}posts.post_type = 'et_pb_layout'
            AND {$wpdb->prefix}term_taxonomy.taxonomy = 'scope'
        ORDER BY {$wpdb->prefix}posts.ID DESC",
        ARRAY_A
    );

How can I combine these two queries into one?

I cannot use normal WordPress commands to return these results because at the time I need them the taxonomies are not yet loaded.

  • Why do you need terms before the taxonomies are loaded? – Jacob Peattie Jun 8 at 11:55
  • I am going under the assumption they are not loaded (get_the_terms returns "invalid taxonomy") because these custom taxonomies get loaded with the theme, which I can't really change. However I need to produce a post list to display before that. – PthPndr Jun 8 at 12:39
  • You can't display a post list before the theme is loaded. That wouldn't make any sense. So why not just run the code later? – Jacob Peattie Jun 8 at 12:41
  • I understand what you're saying, my apologies if I am not expressing myself correctly. The function gets called when an ACF options page loads. I tried loading the page later but it just screwed up other things. Unless I am completely missing an easy way to run that function later? – PthPndr Jun 8 at 13:46

Your Answer

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

Browse other questions tagged or ask your own question.