多种分类法构建自定义帖子网格(wordpress + ACF)

gfttwv5a  于 2023-03-07  发布在  WordPress
关注(0)|答案(1)|浏览(229)

我需要创建一个自定义后查询循环,从各种自定义分类术语集每页与分类字段。
由于我不是一个高级的可湿性粉剂PHP开发人员,因为我需要推进项目的其他方面,我恳请你的帮助,完成建设查询。我几乎有它,但我对混合分类法,把关系和层次结构的条件块。
背景是这样的:网站是一个真实的地产经纪公司,与上市自定义职位和自定义分类提供租金/销售(categorie_offre),属性类型(type_bien)和城市(镇)。属性类型有子分类为房子,公寓,车库,一个房间,两个房间,等等。
在单页编辑器,我有我的分类ACF领域的3个分类,我决定什么术语必须过滤清单网格。
在单页模板中,现在我有这样的代码,它只适用于一个分类,但不适用于多个分类(我现在没有把城市),我认为array_merge不是好的解决方案:
我正在考虑使用这个胎面的想法,但如果一些高级开发人员可以给予我一只手,以实现它更快,我会非常非常非常感激:https://wordpress.stackexchange.com/questions/371737/how-to-get-posts-that-contain-multiple-terms-from-multiple-taxonomies
谢谢
罗兰

<?php
// Liste d'annonces pages SEO

$post_type = 'annonce';
$location_vente = get_field( 'location_ou_vente' );
$type_bien = get_field( 'type_de_bien' );
$termis = array_merge($location_vente,$type_bien);

foreach( $termis as $termi ) :
$argsi = array(
'post_type' => $post_type,
'posts_per_page' => 5,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'categorie_offre',
'field' => 'term_id',
'operator' => 'OR',
'terms' => $location_vente
),
array(
'taxonomy' => 'type_bien',
'field' => 'term_id',
'operator' => 'OR',
'terms' => $type_bien
),
),
);

endforeach;

$annonces = new WP_Query( $argsi );

// Output posts, etc.

while ( $annonces->have_posts() ) : $annonces->the_post();
the_title();
the_excerpt();
echo '<br />';
endwhile;

wp_reset_query();

?>
snz8szmq

snz8szmq1#

也许我的问题不清楚...
不管怎样,这是我找到的解决方案:

$categories = get_field('location_ou_vente');
// make sure it is an array, if only one is
// selected it may return a single value
if (!is_array($categories)) {
  $categories = array($categories);
}
$types = get_field('type_de_bien');
if (!is_array($types)) {
  $types = array($types);
}
$villes = get_field('ville_bien');
if (!is_array($villes)) {
  $villes = array($villes);
}
    
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$postsPerPage = 10;
    
$args = array(
  'post_type' => 'annonce',
  'posts_per_page' => $postsPerPage,
  'paged' => $paged,
  'tax_query' => array(
    'relation' => 'AND',
    array(
      'taxonomy' => 'categorie_offre',
      'relation' => 'OR',
      'terms' => $categories
    ),
    array(
      'taxonomy' => 'type_bien',
      'relation' => 'OR',
      'terms' => $types
    ),
    array(
      'taxonomy' => 'ville',
      'relation' => 'OR',
      'terms' => $villes
    )
  )
);
$annonces = new WP_Query($args);
        $nb_annonces = $annonces->found_posts;
    
    if ($nb_annonces < 2) : 
    echo 'Moins de 2 annonces';
    else :

// loop reuslts here to display posts

        
//    $annonces = new WP_Query( $argsi );

    // Output posts, etc.
    if ( $annonces->have_posts() ) : 
        echo '<div class="annonces">';
        while ( $annonces->have_posts() ) : $annonces->the_post(); 

            get_template_part( 'template-parts/grid', 'annonce' );
        endwhile;
    
    echo '<div class="pagination">';

        echo paginate_links( array(
            'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
            'total'        => $annonces->max_num_pages,
            'current'      => max( 1, get_query_var( 'paged' ) ),
            'format'       => '?paged=%#%',
            'show_all'     => false,
            'type'         => 'plain',
            'end_size'     => 2,
            'mid_size'     => 1,
            'prev_next'    => true,
            'prev_text'    => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
            'next_text'    => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
            'add_args'     => false,
            'add_fragment' => '',
        ) );

echo '</div>';
        echo '</div>';
        echo '<a id="more_posts" href="#">Load More</a>';
        else :

        get_template_part( 'template-parts/content', 'none' );
        
        endif;
        
        
    wp_reset_query();
    
        
        
        
?>

相关问题