最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - How to show only one post for each categories of taxonomy of custom post that contains a specific custom field

programmeradmin0浏览0评论

I have a custom post called 'account' and custom taxonomy called 'bank';

What I'm trying to do is to show with wp_query post a list of post that should have a specific custom fields named 'tax'.

But I want in the result list of posts, only one post (for example the last) for each categories of my custom taxonomy.

My code is:

$query = new WP_Query ( array( 
    'showposts' => 5, 
    'post_type' => 'account', 
    'meta_key' => 'other_custom_field', 
    'orderby' => 'meta_value_num', 
    'order' => 'desc', 
    'meta_query' => array(array('key' => 'tax','compare' => '=','value' => 0)), ) );

This code works but it shows all post with same custom fields 'tax' contained in each category of custom post.

I want to show only one post for each categories, with this value of custom field.

I have a custom post called 'account' and custom taxonomy called 'bank';

What I'm trying to do is to show with wp_query post a list of post that should have a specific custom fields named 'tax'.

But I want in the result list of posts, only one post (for example the last) for each categories of my custom taxonomy.

My code is:

$query = new WP_Query ( array( 
    'showposts' => 5, 
    'post_type' => 'account', 
    'meta_key' => 'other_custom_field', 
    'orderby' => 'meta_value_num', 
    'order' => 'desc', 
    'meta_query' => array(array('key' => 'tax','compare' => '=','value' => 0)), ) );

This code works but it shows all post with same custom fields 'tax' contained in each category of custom post.

I want to show only one post for each categories, with this value of custom field.

Share Improve this question asked Jan 9, 2018 at 19:47 GiulioGiulio 511 silver badge8 bronze badges 1
  • Can someone help me? – Giulio Commented Jan 10, 2018 at 16:42
Add a comment  | 

1 Answer 1

Reset to default 3
<?php
// get all terms of the `bank` taxonomy
$banks = get_terms(
    array(
        'taxonomy' => 'bank',
        // more arguments can be used, see (1) below the code
    )
);

// look through banks, picking one post from each
foreach ( $banks as $bank ) {

    $args = array(
        'post_type'       => 'account', // your custom post type
        'posts_per_page'  => '1', // one post per bank (per term)
        'tax_query'        => array(
            array(
                'taxonomy' => 'bank',
                'terms'    => $bank->term_id,
            ),
        )
        'meta_query' => array( // your custom field stuff
            array(
                'key' => 'tax',
                'compare' => '=',
                'value' => 0,
            ),
        ), 
        // more arguments can be used, see (2) below the code
    );

    // The Loop
    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();

            /*
            Your markup goes here
            */
            the_ID();
            the_title();
        }
    }

} // END foreach bank

Also, here should be the error check, but it's out of the question scope.

For accepted arguments see:

  1. WP_Term_Query.
  2. WP_query

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论