0

I am using the following code 20 times in one page template to query specific data for each section. All of the pages data comes from the same post type, just I need it divided up by a specific custom field value and then order by another specific custom field value. Is there a way to run WP_query just one time and then filter the data into groups based on one custom field value and then orderby another??

<div id="listings">
<?php
        $args = array(
    'post_type' => 'listing',
    'post_status' => 'publish',
    'posts_per_page' => '96',
    'meta_key' => 'price_value',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'meta_query' => array(
                array(
                    'key' => 'category_value',
                    'value' => 'category 1',
                    ),
                array(
                    'key' => 'sold_value',
                    'value' => 'No'
                    )
    )
);

$custom = new WP_Query( $args ); ?>
<h3 class="bar">Category 1 (<?php echo $custom -> found_posts; ?>)</h3>
    <?php $margincounter = 1; ?>
    <?php if ($custom->have_posts()) : while ($custom->have_posts()) : $custom->the_post(); ?>
    <?php include get_template_directory() . '/includes/listings_grid.php';      ?>
    <?php endwhile; else: ?>
    <?php endif; wp_reset_query(); ?>
        </div>
2
  • I think your question is more PHP specific than Wordpress specific. Anyway, you can loop over the results of the query grouping the results by the field value you want divide the results. After that you can sort each group.
    – cybmeta
    Sep 4, 2013 at 20:32

1 Answer 1

1

Wrap your WP_Query object in a function so you don't have to change all the query $args.

The function will pass parameters for custom field value and order by custom field value.

Here how you can do it:

<?php
function custom_query($divided_cf_val=array(), $order_by_cf_val) {
?>
<div id="listings">
<?php
        $args = array(
    'post_type' => 'listing',
    'post_status' => 'publish',
    'posts_per_page' => '96',
    'meta_key' => 'price_value',
    'orderby' => $order_by_cf_val,
    'order' => 'ASC',
    'meta_query' => $divided_cf_val
);

$custom = new WP_Query( $args ); ?>
<h3 class="bar">Category 1 (<?php echo $custom -> found_posts; ?>)</h3>
    <?php $margincounter = 1; ?>
    <?php if ($custom->have_posts()) : while ($custom->have_posts()) : $custom->the_post(); ?>
    <?php include get_template_directory() . '/includes/listings_grid.php';      ?>n in y
    <?php endwhile; else: ?>
    <?php endif; wp_reset_query(); ?>
        </div>
<?php
}
?>

And call this function in your template like this:

<?php
$meta_field1 = array(
                array(
                    'key' => 'category_value',
                    'value' => 'category 1',
                    ),
                array(
                    'key' => 'sold_value',
                    'value' => 'No'
                    )
    );

$meta_field2 = array(
                    array(
                        'key' => 'category_value',
                        'value' => 'category 2',
                        ),
                    array(
                        'key' => 'sold_value',
                        'value' => 'Yes'
                        )
        );

$meta_field3 = array(
                    array(
                        'key' => 'category_value',
                        'value' => 'category 3',
                        ),
                    array(
                        'key' => 'sold_value',
                        'value' => 'Maybe'
                        )
        );

    custom_query($meta_field1, $order_by_cf_val);
    custom_query($meta_field2, $order_by_cf_val);
    custom_query($meta_field3, $order_by_cf_val);
    custom_query($meta_field4, $order_by_cf_val);
?>

and so on...

2
  • Thank You Rahil for the quick answer, one question about your answer. Will your way cut the number of database queries down to one instead of the 20 that I am currently doing? Sep 4, 2013 at 21:30
  • No, it executes the query each time you call the function and echo the results. My code is just shorter way, but if you really want to cut down the number of queries, you should execute all your posts at once and then filter out with the custom field values. It needs a little bit more coding to do that so. Sep 4, 2013 at 21:48

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.