繁体   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