簡體   English   中英

使用自定義條件過濾WordPress循環中的帖子

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

我正在嘗試在我的網站上建立一個帖子過濾器。 帖子頁面需要在頁腳中顯示兩個相似的帖子。 但是他們必須滿足某些條件。

條件:

  • 第一個條件:帖子具有數字值(例如:160),兩個相似的帖子必須在該值范圍內(例如:如果范圍是10,則155值將在160范圍內)。

  • 第二個條件:類似職位與主要職位屬於同一類別

  • 第三個條件:如果前兩個條件失敗,請顯示預定的帖子

循環:

  • 如果第一個條件產生兩個結果,則循環必須停止。

  • 如果第一個條件僅產生一個結果,則循環必須繼續執行第二個條件,以便得到兩個結果(第一個條件匹配第一個條件,第二個條件匹配第二個條件 )。

  • 如果第一個條件循環沒有結果,則對兩個帖子都使用第二個條件

  • 如果第一個第二個失敗使用第三個

問題:

-在第一個條件下 ,循環不會通過所有帖子,而只會通過前兩個最新帖子。 在繼續下一個條件之前,我該如何先使條件遍歷所有帖子?

有人可以向我解釋我如何實現目標,或者至少將我指向正確的方向。 提前致謝

到目前為止,我得到了什么(寬度注釋):

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

分為兩個循環:

$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