dtf54486 2014-11-07 21:21
浏览 20
已采纳

根据每个计算过滤WordPress结果

I am running a WordPress query. To determine which posts I want to display, a calculation needs to be run on each post.

To use an example, let's say all of my posts have a custom field, "my_custom_field". The value is an integer between 1 and 100.

The user then inputs a number between 1 and 100 as well. For each post, we subtract the user's number from the value of my_custom_field. It the result is larger than 10, we display that post in our results.

How do I run that query? I can't seem to figure out which direction to go with this.

Keep in mind, this is just a simplified example of a complicated problem. Assume that we are actually going to need to run a calculation for every post. I realize that in this simple example, you could solve the equation and figure out the range of values that you should display. This won't work in my real-world usage.

Here's an example of how I might want to do it. The problem is, of course, that you can't actually run functions and calculations in the $args array. This is merely to demonstrate what I'm trying to accomplish, I understand that this is not how an $args array works.

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

I've been able to enable or prevent posts from DISPLAYING by basically putting that code form my $args into the display portion of the loop, but of course that's very sloppy and messes horribly with pagination.

I need some way to run that calculation on each post in the WP_Query object and remove all posts that don't meet those criteria. How can I manually filter those results before I actually run the loop?

  • 写回答

2条回答 默认 最新

  • drgd73844 2014-11-18 01:13
    关注

    Figured it out! Essentially the secret is to run two loops. The first one, instead of displaying the results in the loops, runs a calculation on each result and then depending on the outcome of that calculation, adds the post ID to a string. That string is later used as an argument for the second loop, which displays these hand-picked posts.

    So our first loop looks like:

    $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();
    

    Then, for the second loop, we just need to set up our $args array to utilize the array of post IDs we have. Also note, that we have to rename the args array and some variables so they don't overlap with our first loop and cause problems.

    $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();
    

    Boom! There it is. It's actually not too difficult, it's just hard to think of this if you've never done it before. I also have a much more in-depth example on my blog. In it, I use a more practical example of what I'm doing here. I perform a radial search to find locations near the user. It might help explain why you would ever want to do this!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 simulink单相桥式整流电路
  • ¥35 问问51单片机流水灯的代码该怎么写
  • ¥15 关于#百度#的问题:感觉已经将字体段落、字体、页边距、纸张大小、文档网络调成与论文模板一致,为什么黄色部分字体左右的间距还是不一样啊,求私信发文件接收看一下
  • ¥15 stata webuse报错
  • ¥15 TypeError: Cannot read properties of undefined (reading 'status')
  • ¥15 如何利用AI去除图片中的竹架子
  • ¥15 python 写个基金爬取的代码,自动卖出功能
  • ¥15 Linux系统启动不起来
  • ¥15 为什么运行仿真数码管不亮(语言-c语言)
  • ¥15 陈仁良《直升机飞行动力学》小扰动线化方程如何推导