简体   繁体   English

使用自定义条件过滤WordPress循环中的帖子

[英]Filter posts in a WordPress loop with custom conditions

I am trying to build a post filter on my website. 我正在尝试在我的网站上建立一个帖子过滤器。 The post page needs to display two similar posts in the footer. 帖子页面需要在页脚中显示两个相似的帖子。 But they have to meet certain conditions. 但是他们必须满足某些条件。

Conditions: 条件:

  • First condition: A post has a number value(example:160) the two similar posts must be with in range of that value (example: if range is 10 a post with a 155 value would be in range of 160 ). 第一个条件:帖子具有数字值(例如:160),两个相似的帖子必须在该值范围内(例如:如果范围是10,则155值将在160范围内)。

  • Second condition: The similar posts are of the same category as the main post 第二个条件:类似职位与主要职位属于同一类别

  • Third condition: If first two conditions fail, show predetermined post 第三个条件:如果前两个条件失败,请显示预定的帖子

Loops: 循环:

  • if the first condition yields two results the loop must stop. 如果第一个条件产生两个结果,则循环必须停止。

  • if the first condition yields only one result the loop must continue with Second condition so two result get showen(the first post matching the first condition and the second post matching the second condition ). 如果第一个条件仅产生一个结果,则循环必须继续执行第二个条件,以便得到两个结果(第一个条件匹配第一个条件,第二个条件匹配第二个条件 )。

  • if the first condition loop yields no results use the second condition for both posts. 如果第一个条件循环没有结果,则对两个帖子都使用第二个条件

  • if the first and the second fail use the third . 如果第一个第二个失败使用第三个

Problems: 问题:

-The loop does not go trough all posts on the first condition it just goes through the first two lastest posts. -在第一个条件下 ,循环不会通过所有帖子,而只会通过前两个最新帖子。 How I can a make the conditions go through all posts first before continuing to the next condition? 在继续下一个条件之前,我该如何先使条件遍历所有帖子?

Could someone explain to me how I can achieve my goal or atleast point me in the right direction. 有人可以向我解释我如何实现目标,或者至少将我指向正确的方向。 Thanks in advance 提前致谢

What I have got so far (width comments): 到目前为止,我得到了什么(宽度注释):

 <?php

    $orig_post = $post;
    $orig_city = get_post_meta( $post->ID, 'property_city', true );

    // The post range value for main post
    $orig_area = (int)str_replace( array( ' ', ',', '.' ), '', get_post_meta( $post->ID, 'property_area', true ) );

    // The post category for main post
    $orig_category = wp_get_post_terms( $post->ID, 'property_category', array( 'fields' => 'ids' ) );


    $exclude_ids = array( $post->ID );

    // display all posts
    $args = array(
        'posts_per_page' => -1,
        'post_type' => 'property',
        'post_status' => 'publish',
        'post__not_in' => $exclude_ids,

    );


    if ( $my_query->have_posts() && $orig_category ) {

        // Counter to display only two posts
        $counter = 0;
        while ( $my_query->have_posts() and $counter < 1 ) {
            $counter++;
            $my_query->the_post();
            $s_id = get_the_ID();

            // The range
            $s_area = (int)str_replace( array( ' ', ',', '.' ), '', get_post_meta( $s_id, 'property_area', true ) );
            $min = $orig_area - 10;
            $max = $orig_area + 10;

            // The first condition
            if ( ( $min <= $s_area ) and ( $s_area <= $max ) ) {
                echo 'first condition met';
                // The second condition
            } elseif ( $orig_category ) {
                echo 'second condition met';
                // The third condition
            } else {
                echo 'third condition met';
            }
            ?>

        <?php } ?>


    <?php }

    $post = $orig_post;
    wp_reset_query();
    ?>

Split to two loops: 分为两个循环:

$matches = [];
$total = [];

while ( $my_query->have_posts() ) {

    $post = $my_query->the_post();

    if (conformsToFirst($orig_post, $post)) 
        $matches[] = $post;
    else
        $total[] = $post;

    if (enough($orig_post, $matches))
        break;

}

if (!enough($orig_post, $matches)) {
    foreach ($total as $post) {
        if (conformsToSecond($orig_post, $post))
            $matches[] = $post;

        if (enough($orig_post, $matches))
            break;
    }

    if (!enough($orig_post, $matches)) {

        $matches = [pick_predefined_post()];
    }
}

// filtered results
// either [first_condition, first_condition]
// [first_condition, second_condition]
// [second_condition, second_condition, ]
// or [pick_predefined_post()-ed]
var_dump($matches);


function enough($to, $matches) {
    return count($matches) >= 2;
}

function conformsToFirst($to, $post) {
    //
    if (...)
        return true;

    return false;
}

function conformsToSecond($to, $post) {
    //
    if (...)
        return true;

    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM