Wordpress预先获取帖子按元值序列化数组的顺序

时间:2020-07-23 05:41:02

标签: wordpress get posts pre

我有一个自定义帖子类型(cars)和自定义分类法(modelscountries

每个countries的term_meta都像nearly_countries一样,例如,我有loaction 法国,在nearly_countries 意大利西班牙

创建帖子时,我要添加

  • post_meta = position白色我想要的位置数量 分类法models
  • post_meta = avg_rating关于评分
  • post_meta = all_position,带有序列化 array('term_id_of_model'=>'position_in_model', 'term_id_of_model_term_id_of_country'=>'position_in_model_filter_by_country', 'term_id_of_model_term_id_of_nearly_country'=>'position_in_model_filter_by_nearly_country', 'term_id_of_model_term_id_of_nearly_country'=>'position_in_model_filter_by_nearly_country')

对于下面的代码,按我预期的那样工作

$taxquery = array();

if (isset($_GET['loc']) && (!empty($_GET['loc']))) {
    $term = get_term_by('slug', $_GET['loc'], 'countries');
    $term_id = $term->term_id;

    $taxquery[] = array(
        'taxonomy'  => 'countries',
        'field'     => 'term_id',
        'terms'     => array($term_id),
        'operator'  => 'IN'
   );
}
$query->set('tax_query', $taxquery);
$query->set( 'meta_query', array(
    'position' => array(
       'key' => 'position'
     ),
     'avg_rating' => array(
        'key' => 'avg_rating'
     )
));
$query->set( 'orderby', array( 'position' => 'ASC', 'avg_rating' => 'DESC', 'rand'=>'DESC' ));
return $query;

然后我尝试输入和定位,但是我需要用position替换meta_key all_postion并对其进行过滤

$taxquery = array();

if (isset($_GET['loc']) && (!empty($_GET['loc']))) {
    $term = get_term_by('slug', $_GET['loc'], 'countries');
    $term_id = $term->term_id;
    
    $get_nearly = get_term_meta( $term_id, '_nearly_location', true );
    if (!empty($get_nearly)) { 
        $loc_arr = $term_id.','.$get_nearly;
        $countries = explode(",", $loc_arr);
                
        $taxquery[] = array(
            'taxonomy'  => 'countries',
            'field'     => 'term_id',
            'terms'     => $countries,
            'operator'  => 'IN'
        );
        
    }
    else {
        $taxquery[] = array(
            'taxonomy'  => 'countries',
            'field'     => 'term_id',
            'terms'     => array($term_id),
            'operator'  => 'IN'
            );  
    }
}
$query->set('tax_query', $taxquery);
$query->set( 'meta_query', array(
    'position' => array(        // i want to
        'key' => 'position'     // replace these
    ),                          // three lines
    'avg_rating' => array(
        'key' => 'avg_rating'
    )
));
$query->set( 'orderby', array( 'position' => 'ASC', 'avg_rating' => 'DESC', 'rand'=>'DESC' ));
return $query;

以下是all_position元值s:236:"a:17:{i:53;i:4;s:6:"53_263";i:1;s:3:"53_";i:4;i:408;i:4;s:4:"408_";i:4;i:13;i:4;s:3:"13_";i:4;i:12;i:4;s:3:"12_";i:4;i:406;i:4;s:4:"406_";i:4;i:407;i:4;s:4:"407_";i:4;s:6:"53_434";i:4;s:6:"53_442";i:4;s:6:"53_454";i:4;s:6:"53_384";i:4;}";

的示例
Array
(
    [53] => 4
    [53_263] => 1
    [53_] => 4
    [408] => 4
    [408_] => 4
    [13] => 4
    [13_] => 4
    [12] => 4
    [12_] => 4
    [406] => 4
    [406_] => 4
    [407] => 4
    [407_] => 4
    [53_434] => 4
    [53_442] => 4
    [53_454] => 4
    [53_384] => 4
)

其中53是模型53_263是国家,而53_43453_44253_45453_384几乎是国家/地区

我该如何订购?也许我必须更改meta_value all_position。我不想将它们每个保存在不同的post_meta

对不起,我的英语水平

0 个答案:

没有答案