簡體   English   中英

如何使用PHP在wordpress中按特色圖片(底部無圖片)對帖子排序?

[英]How can I sort posts by featured image(no image posts at the bottom) in wordpress using PHP?

我正在使用WP類型/視圖工具集處理Wordpress查詢對象。 http://wp-types.com

我們建立了參數搜索,允許用戶使用各種分類法搜索帖子。 它工作正常,可以根據需要顯示搜索結果。 但..

大多數帖子沒有特色圖片,我們希望在頂部顯示特色圖片,而特色無圖片的帖子會在下方。

就邏輯而言,我在這方面有一個很好的起點,只需要一點幫助。

下面的代碼允許我在將查詢呈現給用戶之前操​​縱查詢。

add_filter( 'wpv_filter_query_post_process', 'prefix_rearrange_by_thumbnail', 10, 3 );

    function prefix_rearrange_by_thumbnail( $query, $view_settings, $view_id ) {
        // sort posts by thumbnail
    return $query;
}

我該如何對query-> post進行排序並重新排列它們,以便那些具有特色圖像的圖像比那些沒有圖像的圖像更早顯示。

所有WP_Post對象都位於$query->posts下的數組中。 您可以根據需要使用usort()對數組進行排序。 請記住,這只會對結果的每一頁進行排序,因為這就是返回的結果。

add_filter( 'wpv_filter_query_post_process', 'prefix_rearrange_by_thumbnail' );
function prefix_rearrange_by_thumbnail( $query ) {
    $posts = $query->posts;
    usort( $posts, 'sort_by_thumbnail' );
    return $query;
}

// function used to sort the posts array
function sort_by_thumbnail( $a, $b ){
    // the the featured image id so we can sort by it
    $a_thumb = get_post_meta( $a->ID, '_thumbnail_id', true );
    $b_thumb = get_post_meta( $b->ID, '_thumbnail_id', true );

    if ( empty( $a_thumb ) && ! empty( $b_thumb ) ) return 1;
    if ( ! empty( $a_thumb ) && empty( $b_thumb ) ) return -1;
    return 0;
}

要對結果的所有頁面進行排序,您需要使用wpv_filter_query過濾器修改傳遞給WP_Query的參數。 每個具有特色圖片的帖子/頁面都有一個名為“ _thumbnail_id”的meta_key,但是問題是,沒有特色圖片的帖子/頁面根本沒有設置此meta_key,因此如果使用它,結果將是按特色圖片的ID排序,但是如果缺少,則將其省略。 一種選擇是根據要使用的meta_key設置另一個標志。 需要清理一次,然后可以在save_post上使用鈎子

一次SQL(根據您的數據庫前綴進行調整):

INSERT INTO wp_postmeta( post_id, meta_key, meta_value )
SELECT p.ID, '_has_featured_image', IF ( meta_value IS NULL, 0, 1 ) AS _has_featured_image
FROM wp_posts p
LEFT JOIN wp_postmeta m ON p.ID  = m.post_id AND meta_key = '_thumbnail_id'
WHERE p.post_status = 'publish'
AND p.post_type IN ('post','page')

鈎上save_post以使“ _has_featured_image”保持干凈:

add_action( 'save_post', 'set_has_featured_image' );
function set_has_featured_image( $post_id ){
    $post = get_post( $post_id );
    switch ( $post->post_type ){
        case 'post': case 'page':
            $thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true );
            update_post_meta( $post_id, '_has_featured_image', ! empty( $thumbnail_id ) );
        break;
    }
}

現在,您可以過濾“ _has_featured_image” meta_key

add_filter( 'wpv_filter_query', 'prefix_rearrange_by_thumbnail' );
function prefix_rearrange_by_thumbnail( $args ){ 
    $args['orderby'] = 'meta_value';
    $args['meta_key'] = '_has_featured_image';
    return $args;
}

我設法以一種更簡單的方式來做...

add_filter( 'wpv_filter_query_post_process', 'prefix_rearrange_by_thumbnail', 10, 3 );
function prefix_rearrange_by_thumbnail( $query, $view_settings, $view_id ) {
    if ( !empty( $query->posts ) ) {

        $sorted_posts_top = array();
        $sorted_posts_bottom = array();
        $all_posts = $query->posts;

        foreach ($all_posts as $post) {

            if ( has_post_thumbnail($post->ID) ) {
                $sorted_posts_top[] = $post;
                }
            else {
                $sorted_posts_bottom[] = $post;
            }

          $query->posts = array_merge((array)$sorted_posts_top, (array)$sorted_posts_bottom);

        }

    }
    return $query;
}

但是,您的答案仍然非常有幫助。 非常感謝你的分享!!!!!!!!!!!!!!!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM