Query seems to be duplicated

时间:2014-04-10 作者:Dannyw24

我刚刚遇到了发展瓶颈,似乎找不到解决这个问题的办法。

我已经运行了两个WP\\U查询,目的是一个显示高级库存商,另一个显示非高级库存商。我添加了一个勾选框,允许我使用一个条件来检查它是否是优质库存商。

如果用户是高级会员,如何在*中添加正确的逻辑显示高级股票持有人。这些必须出现在页面顶部,高于其他库存商,因为他们是溢价的。

我正在使用一个名为高级自定义字段的插件来生成高级真/假字段。网站是enter link description here

我有代码。

<?php

$args = array(
    \'orderby\' => \'title\',
    \'order\' => \'ASC\',
    \'posts_per_page\' => 5,
    \'paged\' => get_query_var(\'paged\'),
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'store-category\',
            \'field\' => \'id\',
            \'terms\' => array($_POST[\'region\'])
            )
        )
    );

$query = new WP_Query($args);

if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();

$mapurl = get_post_meta($post->ID, \'lj_mapurl\', true); ?>

<div class="usercontent">
    <div class="stockist-content green-bg">
        <strong><?php the_title(); ?></strong>
        <?php the_content(); ?>
    </div>
    <div class="stockist-image">
        <?php   if ( has_post_thumbnail() ) {
            $postthumnail_count++;
            the_post_thumbnail(\'stock\');
        }
        ?> 
    </div>
</div>
<div class="clear" style="clear:both;"></div>
<div class="clr">&nbsp;</div>
<a class="viewMap" href="<?php echo $mapurl; ?>" title="Find <?php the_title(); ?> on a map (opens in a new window)" target="_blank">View Map</a>
<div class="clr">&nbsp;</div>
<hr />
<?php endwhile; ?>
<?php else: ?>
    <p>Sorry, content not found</p>

<?php endif; ?>
<?php wp_reset_query(); ?>

<?php

$args = array(
    \'orderby\' => \'title\',
    \'order\' => \'ASC\',
    \'posts_per_page\' => 5,
    \'paged\' => get_query_var(\'paged\'),
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'store-category\',
            \'field\' => \'id\',
            \'terms\' => array($_POST[\'region\'])
            )
        )
    );

$query = new WP_Query($args);

if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();

$mapurl = get_post_meta($post->ID, \'lj_mapurl\', true); 
$premium = get_field(\'premium_stock_supplier\');
if ( ! $premium ) { // this is basically short for $premium == false
    ?>

    <div class="usercontent">
        <div class="stockist-content">
            <strong><?php the_title(); ?></strong>
            <?php the_content(); ?></div></div>


            <a class="viewMap" href="<?php echo $mapurl; ?>" title="Find <?php the_title(); ?> on a map (opens in a new window)" target="_blank">View Map</a>
            <div class="clr">&nbsp;</div>
            <hr />
            <?php  } ?>



        <?php endwhile; ?>
    <?php else: ?>
    <p>Sorry, content not found</p>

<?php endif; ?>
用于显示高级库存商的更新代码(不起作用)

<?php
              $args = array(
    \'orderby\' => \'title\',
    \'order\' => \'ASC\',
    \'meta_query\' => array(
        array(
            \'meta_key\' => \'premium\',
            \'meta_value\' => \'0\',
            )
        ),
    \'posts_per_page\' => 5,
    \'paged\' => get_query_var(\'paged\'),
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'store-category\',
            \'field\' => \'id\',
            \'terms\' => array($_POST[\'region\'])
            )
        )
    );
显示无优质库存商的代码(不起作用)

$args = array(
    \'orderby\' => \'title\',
    \'order\' => \'ASC\',
    \'meta_query\' => array(
        array(
            \'meta_key\' => \'premium\',
            \'meta_value\' => \'0\',
            )
        ),
    \'posts_per_page\' => 5,
    \'paged\' => get_query_var(\'paged\'),
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'store-category\',
            \'field\' => \'id\',
            \'terms\' => array($_POST[\'region\'])
            )
        )
    );

1 个回复
SO网友:Tom J Nowell

您的查询显示相同的输出,因为它们是相同的

以下是您的第一个查询参数:

$args = array(
    \'orderby\' => \'title\',
    \'order\' => \'ASC\',
    \'posts_per_page\' => 5,
    \'paged\' => get_query_var(\'paged\'),
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'store-category\',
            \'field\' => \'id\',
            \'terms\' => array($_POST[\'region\'])
        )
    )
);
以下是您的第二个查询参数:

$args = array(
    \'orderby\' => \'title\',
    \'order\' => \'ASC\',
    \'posts_per_page\' => 5,
    \'paged\' => get_query_var(\'paged\'),
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'store-category\',
            \'field\' => \'id\',
            \'terms\' => array($_POST[\'region\'])
        )
    )
);
你两次都要求同样的东西,你两次都得到同样的东西。

由于您使用的是ACF,您应该通过post meta查询您的ACF字段:

e、 g.类似于此:

$args = array(
    \'meta_key\' => \'premium\', // I guessed your field would be called premium, but this might not be the case
    \'meta_value\' => \'yes\'
);

// get results
$the_query = new WP_Query( $args );

... etc..


$args = array(
    \'meta_key\' => \'premium\',
    \'meta_value\' => \'no\'
);

// get results
$the_query = new WP_Query( $args );
ACF字段也是后元/自定义字段,它们只是包装在一个奇特的UI/API后面

结束

相关推荐

Get_Locale()在同一函数.php文件中行为异常

我正在为我的客户开发一个多语言的门户网站-英语和孟加拉文。我没有使用multisite,而是安装了Polylang插件。通过该网站,它运行良好。对于不同的CSS样式和不同的字体大小,我将加载一个单独的样式表,其中包含:function styles_and_scripts(){ wp_register_style( \'main-style-bangla\', get_template_directory_uri() . \'/style-bangla.css\', \'\', \'\',