【问题标题】:Filter WordPress Results Based on Calculations for Each根据每个计算过滤 WordPress 结果
【发布时间】:2015-01-04 18:41:27
【问题描述】:

我正在运行 WordPress 查询。要确定我要显示哪些帖子,需要对每个帖子进行计算。

举个例子,假设我所有的帖子都有一个自定义字段“my_custom_field”。该值为 1 到 100 之间的整数。

然后用户也输入一个介于 1 和 100 之间的数字。对于每个帖子,我们从 my_custom_field 的值中减去用户编号。如果结果大于 10,我们会在结果中显示该帖子。

如何运行该查询?我似乎无法弄清楚该往哪个方向发展。

请记住,这只是一个复杂问题的简化示例。假设我们实际上需要对每个帖子进行计算。我意识到在这个简单的例子中,你可以解方程并找出你应该显示的值的范围。这在我的实际使用中不起作用。

这是我可能想要做的一个例子。当然,问题在于您实际上无法在 $args 数组中运行函数和计算。 这只是为了展示我想要完成的工作,我知道这不是 $args 数组的工作方式。

$args = array ( $a = get_field('my_custom_field'); //I'm using ACF which uses get_field, not important
                $b = $user_inputted_field //The value the user entered

         Display posts where: $a - $b > 10 //Not real code... Just showing my intention 
);


$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
//Display our posts
}

我已经能够通过基本上将我的 $args 中的代码放入循环的显示部分来启用或阻止帖子显示,但当然这非常草率并且与分页非常混乱。

我需要某种方法来对 WP_Query 对象中的每个帖子运行该计算,并删除所有不符合这些条件的帖子。如何在实际运行循环之前手动过滤这些结果?

【问题讨论】:

  • 有了这么少的信息很难回答你的问题。尝试更准确地回答您的问题,并包括您为实现目标所做的工作。这样人们就可以更轻松地回答您的问题。
  • 已编辑以使其更加清晰。我将不得不以某种方式通过 WP_Query 对象,对每个帖子运行计算,然后返回一个修改后的对象,只留下符合我条件的帖子。

标签: php wordpress


【解决方案1】:

想通了!本质上,秘诀是运行两个循环。第一个不是在循环中显示结果,而是对每个结果进行计算,然后根据该计算的结果,将帖子 ID 添加到字符串中。该字符串稍后用作第二个循环的参数,显示这些精选的帖子。

所以我们的第一个循环看起来像:

$results = array(); //Set up our array, empty for now
$the_query = new WP_Query( $args );


// The Loop
$posts = $the_query->get_posts();

foreach($posts as $post)  {

$a = get_field('my_custom_field'); //I'm using ACF which uses get_field, not important
$b = $user_inputted_field



if ( $a - $b > 10 ) { 

array_push($results, get_the_ID()); //Add the ID of this post to our array

}}        

                    wp_reset_postdata();

然后,对于第二个循环,我们只需要设置我们的 $args 数组来利用我们拥有的帖子 ID 数组。另请注意,我们必须重命名 args 数组和一些变量,以免它们与我们的第一个循环重叠并导致问题。

$args2 = array(
         'post_type' => 'page', //I'm looking for pages, you might not be
         'post__in' => $results
         );

$the_query2 = new WP_Query( $args2 );


// The Loop
if ( $the_query2->have_posts() ) { 
while ( $the_query2->have_posts() ) {
$the_query2->the_post(); ?>

                //Do something. This is where you would DISPLAY the posts

<?php
} }
/* Restore original Post Data */
wp_reset_postdata();

轰隆隆!它在那里。这实际上并不太难,如果您以前从未做过,很难想到这一点。我的博客上还有一个更深入的例子。在其中,我使用了一个更实际的例子来说明我在这里所做的事情。我执行radial search to find locations near the user。它可能有助于解释为什么你会想要这样做!

【讨论】:

  • 完美!我试图在 Wordpress 中实现类似的概念(使用 WC Vendors 插件),并希望在用户地址的交付半径内显示供应商列表 [基于纬度和长地理坐标],但它太复杂而无法尝试实现使用标准的 wp_query/meta_query。
【解决方案2】:

你可以试试这样的:

$args = array(
 'meta_query'=> array(
   array(
    'key' => 'my_custom_field',
    'compare' => '>',
    'value' => 10, // put here what the user inputs
    'type' => 'numeric',
   )
 ),
 'posts_per_page' => -1
) );

query_posts( $args );

【讨论】:

  • 这只是简单地检查并确保自定义字段的值大于 10,这不是我们想要做的。我们需要使用用户输入的值对 my_custom_field 的值进行计算。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-23
  • 2016-09-05
  • 1970-01-01
  • 1970-01-01
  • 2022-10-03
  • 2021-03-07
  • 2015-05-26
相关资源
最近更新 更多