can these 3 queries be re-written as 1 query?

I am creating a Dentist directory site, and I have a taxonomy archive page which requires particular grouping and sorting (see below). So far I have only been able to achieve this using 3 WP_Querys and 3 loops. This does work fine, except of course WP pagination is broken.

What I’d really like to figure out is if there is anyway that these 3 queries could be re-written as one query so that pagination in WP works as normal. I wonder if some how a single wp_query (and perhaps use WP filters too: http://codex.wordpress.org/Class_Reference/WP_Query#Filters) could achieve the same results in a single wp_query object?

Read More

I will try to illustrate the sorting and grouping:

first display:

  • taxonomy ‘class’ == ‘premium’
  • meta_value ‘listing_gallery_active’ == true
  • order by meta_value ‘weighting’, then RAND

then display:

  • taxonomy ‘class’ == ‘premium’
  • meta_value ‘listing_gallery_active’ == false
  • order by meta_value ‘weighting’, then RAND

then display:

  • taxonomy ‘class’ NOT IN ‘premium’
  • order by meta_value ‘weighting’, then RAND

Thanks in advance for your kind assistance.

Query 1:

    $premium_gallery_listings_args = array(

            'tax_query' => array(

                array(
                    'taxonomy' => $taxonomy_archive_query_obj -> taxonomy,
                    'field' => 'slug',
                    'terms' => $taxonomy_archive_query_obj -> slug
                ),
                // show premium only
                array(
                    'taxonomy' => 'class',
                    'field' => 'slug',
                    'terms' => 'premium'
                )
            ),
            'meta_query' => array( array(

                    'key' => 'listing_gallery_active',
                    'value' => '1',
                    'compare' => '=='
                )),
            'post_type' => 'dentist',
            'posts_per_page' => $max_results_per_page,
            'paged' => get_query_var('paged'),

            'orderby' => 'meta_value_num RAND',
            'order' => 'DESC',
            'meta_key' => 'weighting',
            'meta_value_num' => '0',

            'meta_compare' => '>'
        );

Query 2:

            // premium listings w/o gallery
    $premium_listings_args = array(

        'tax_query' => array(

            array(
                'taxonomy' => $taxonomy_archive_query_obj -> taxonomy,
                'field' => 'slug',
                'terms' => $taxonomy_archive_query_obj -> slug
            ),
            // show premium only
            array(
                'taxonomy' => 'class',
                'field' => 'slug',
                'terms' => 'premium'
            )
        ),
        'meta_query' => array( array(

                'key' => 'listing_gallery_active',
                'value' => '1',
                'compare' => '!='
            )),
        'post_type' => 'dentist',
        'posts_per_page' => $max_results_per_page,

        'paged' => get_query_var('paged'),

        'orderby' => 'meta_value_num RAND',
        'order' => 'DESC',
        'meta_key' => 'weighting',
        'meta_value_num' => '0',

        'meta_compare' => '>'
    );

Query 3:

        // standard listings
        $standard_listings_args = array(

            'tax_query' => array(

                array(
                    'taxonomy' => $taxonomy_archive_query_obj -> taxonomy,
                    'field' => 'slug',
                    'terms' => $taxonomy_archive_query_obj -> slug
                ),
                // exclude premium listings
                array(
                    'taxonomy' => 'class',
                    'field' => 'slug',
                    'terms' => 'premium',
                    'operator' => 'NOT IN'
                )
            ),
            'post_type' => 'dentist',
            'paged' => get_query_var('paged'),
            'posts_per_page' => $max_results_per_page,
            'orderby' => 'rand'
        );

Related posts

Leave a Reply