简体   繁体   English

如何使用PHP在wordpress中按特色图片(底部无图片)对帖子排序?

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

I am working with the Wordpress Query Object using the WP Types/Views Toolset. 我正在使用WP类型/视图工具集处理Wordpress查询对象。 http://wp-types.com http://wp-types.com

We built out the parametric search which allows the user to search through posts using various taxonomies. 我们建立了参数搜索,允许用户使用各种分类法搜索帖子。 It works fine, it display search results as needed. 它工作正常,可以根据需要显示搜索结果。 BUT.. 但..

Most of the posts don't have featured images, we want to display posts with featured images at the top, and the featured imageless posts would go below. 大多数帖子没有特色图片,我们希望在顶部显示特色图片,而特色无图片的帖子会在下方。

I have a good head start on this as far as logic goes, just need a bit of help. 就逻辑而言,我在这方面有一个很好的起点,只需要一点帮助。

The code below allows me manipulate the query before rendering it to the user. 下面的代码允许我在将查询呈现给用户之前操​​纵查询。

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

How can I sort through the query->posts and rearrange them so the ones that do have featured images show up before those without. 我该如何对query-> post进行排序并重新排列它们,以便那些具有特色图像的图像比那些没有图像的图像更早显示。

All of the WP_Post objects are found in an array under $query->posts . 所有WP_Post对象都位于$query->posts下的数组中。 You can sort the array using usort() based on whatever criteria you want. 您可以根据需要使用usort()对数组进行排序。 Keep in mind that this will only sort each page of results as that is what is returned. 请记住,这只会对结果的每一页进行排序,因为这就是返回的结果。

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

To sort all the pages of the results you need to modify the arguments that are passed to WP_Query using the wpv_filter_query filter. 要对结果的所有页面进行排序,您需要使用wpv_filter_query过滤器修改传递给WP_Query的参数。 There is a meta_key called "_thumbnail_id" set for each post/page that has a featured image, but the issue is that this meta_key is not set at all for posts/pages without a featured image, so if you use it the results will be sorted by the ID of the featured images, but if it is missing they will be omitted. 每个具有特色图片的帖子/页面都有一个名为“ _thumbnail_id”的meta_key,但是问题是,没有特色图片的帖子/页面根本没有设置此meta_key,因此如果使用它,结果将是按特色图片的ID排序,但是如果缺少,则将其省略。 One option is to set another flag based on that meta_key to use in your sort. 一种选择是根据要使用的meta_key设置另一个标志。 A one time cleaned would be needed then you could use a hook on save_post 需要清理一次,然后可以在save_post上使用钩子

One time SQL (adjust for your db prefix): 一次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')

Hook on save_post to keep "_has_featured_image" clean going forward: 钩上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;
    }
}

Now you can filter on the "_has_featured_image" meta_key : 现在,您可以过滤“ _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;
}

I managed to do it in an easier way... 我设法以一种更简单的方式来做...

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

However, your answer is still VERY helpful. 但是,您的答案仍然非常有帮助。 Thank you SO MUCH for sharing!!!!!!!!!!!!!!!! 非常感谢你的分享!!!!!!!!!!!!!!!!

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

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