我可以使用WP_QUERY一次,然后多次过滤结果吗

时间:2013-09-04 作者:user1680701

我在一个页面模板中使用以下代码20次,以查询每个部分的特定数据。所有页面数据都来自同一个帖子类型,只是我需要将其除以一个特定的自定义字段值,然后按另一个特定的自定义字段值排序。是否有一种方法可以只运行一次WP\\U查询,然后根据一个自定义字段值将数据过滤到组中,然后按另一个字段值排序??

<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>

1 个回复
SO网友:Rahil Wazir

将WP\\u查询对象包装在函数中,这样就不必更改所有查询$参数。

该函数将传递自定义字段值的参数和按自定义字段值排序的参数。

以下是您的操作方法:

<?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
}
?>
并在模板中调用此函数,如下所示:

<?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);
?>
等等。。。

结束

相关推荐

JQuery在WordPress编辑器上不起作用

我已经在WP的编辑器中添加了一些内容,但是新内容对点击行为没有反应。下面是我如何添加内容的:add_filter( \'default_content\', \'custom_editor_content\' );然后创建了我的函数custom_editor_content:function custom_editor_content( $content ) { $content = \' <button id=\"test\">Add</b