PHP 带有排序选项的WordPress的自定义分类法过帐列表过滤器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 带有排序选项的WordPress的自定义分类法过帐列表过滤器相关的知识,希望对你有一定的参考价值。

// Get custom taxonomies
// Get array of taxonomies eg activity, difficulty, subject
$taxonomies = get_taxonomies( array('_builtin' => false, 'public' => true),'objects' ); 
// foreach taxonomy, get a list of options to sort by, terms, eg maths, science
foreach ( $taxonomies as $tax => $tax_obj ){ 
    $terms[$tax_obj->query_var] = get_terms($tax_obj->name); // array of terms
}

// check stuff in GET is a registered taxonomy
$tax_args = array();
foreach ( $_GET as $key => $val ){
    if( array_key_exists($key, $terms) ){ 
        // if its set, add an argument for get_posts
        if( $val ){
            $tax_args[$key] = $val;
        }
    }
}
        


// build lines of conditions for if there are taxonomical requirements in the query string
$sort_conditions = '';
$required_matches = -1;

if( count( $tax_args ) ){
    $sort_conditions.= " WHERE 0=1 "; // add this so other stuff can just repeat ORs
    foreach( $tax_args as $key => $slug ){
        $sort_conditions.= " OR taxonomy = '$key' AND slug = '$slug' ";
        $required_matches ++;
    }
}

$sql ="
SELECT count(1) AS matches,t1.* FROM(

    SELECT wp_posts.*, wp_term_taxonomy.taxonomy, wp_terms.slug
        FROM wp_posts
            LEFT JOIN wp_term_relationships ON(wp_posts.ID = wp_term_relationships.object_id)
            LEFT JOIN wp_term_taxonomy ON(wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
            LEFT JOIN wp_terms ON(wp_term_taxonomy.term_id = wp_terms.term_id)
            JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
    WHERE wp_posts.post_type = 'post'
        AND (wp_posts.post_status = 'publish')
        AND wp_postmeta.meta_key = 'priority'
        AND wp_posts.post_status = 'publish'
    ORDER BY wp_postmeta.meta_value ASC, wp_posts.post_date DESC
    LIMIT 0, 999) AS t1

    $sort_conditions
GROUP BY ID HAVING matches > $required_matches";

$listing = $wpdb->get_results( $sql );

以上是关于PHP 带有排序选项的WordPress的自定义分类法过帐列表过滤器的主要内容,如果未能解决你的问题,请参考以下文章

带有排序选项的WordPress自定义分类后列表过滤器

Wordpress - 如何通过其分类过滤添加的自定义帖子?

根据带有加载更多按钮的自定义查询获取 WordPress 帖子

Wordpress 在自定义帖子类型存档页面上按日期排序

带有假 postmeta 的 Wordpress 假帖子。

php WordPress的自定义翻译