最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Custom post type - list posts based on meta_key and display them divided by CPT taxonomy category

matteradmin11PV0评论

Shortly:

  1. I've custom post type called 'produkty'
  2. Inside this CPT I've taxonomy called 'kategorie_produktow' with terms (list of product categories, like in normal posts) - each product has ONE category
  3. Inside this CPT I've custom field called 'produkt_nowosc' - it is used to describe the product as newest in offer
  4. I'm using a custom page template to display all CPT with 'product_new' set as 'tak' (yes)

To display posts describe above I'm using normal WP_Query():

$the_query = new WP_Query( array( 'post_type' => 'produkty', 'meta_key' => 'produkt_nowosc', 'meta_value' => 'tak', 'posts_per_page' => -1) ) ;

It works perfect for displaying all products, with every product category (taxonomy and term). [product 1] [product 2] [product 3] [product 4]

Now I'd like to separate each CPT taxonomy, it's term and display posts with produkt_nowosc set to tak like this:

Post category 1:

[product 1] [product 2] [product 3]

Post category 2:

[product 4]

What type of query should I use?

Shortly:

  1. I've custom post type called 'produkty'
  2. Inside this CPT I've taxonomy called 'kategorie_produktow' with terms (list of product categories, like in normal posts) - each product has ONE category
  3. Inside this CPT I've custom field called 'produkt_nowosc' - it is used to describe the product as newest in offer
  4. I'm using a custom page template to display all CPT with 'product_new' set as 'tak' (yes)

To display posts describe above I'm using normal WP_Query():

$the_query = new WP_Query( array( 'post_type' => 'produkty', 'meta_key' => 'produkt_nowosc', 'meta_value' => 'tak', 'posts_per_page' => -1) ) ;

It works perfect for displaying all products, with every product category (taxonomy and term). [product 1] [product 2] [product 3] [product 4]

Now I'd like to separate each CPT taxonomy, it's term and display posts with produkt_nowosc set to tak like this:

Post category 1:

[product 1] [product 2] [product 3]

Post category 2:

[product 4]

What type of query should I use?

Share Improve this question edited May 15, 2015 at 7:43 knysha asked May 14, 2015 at 12:24 knyshaknysha 214 bronze badges 4
  • You can try this approach. Just a note, this uses built in categories and the deafult posts, so you will need to modify the code accordingly – Pieter Goosen Commented May 15, 2015 at 7:55
  • 1 Thank you Pieter! I've changed the code to matche the custom post type and taxonomies and voila! pastebin/xKBepGQz – knysha Commented May 15, 2015 at 8:13
  • You can post your code and what you did as an answer and accept your own answer. The are a badge for this as well :-) – Pieter Goosen Commented May 15, 2015 at 8:15
  • @PieterGoosen - oh, I didn't know that :) – knysha Commented May 15, 2015 at 8:43
Add a comment  | 

1 Answer 1

Reset to default 2

Here's the code, rewriten from posts and categories to custom post types and taxonomy terms.

Thanks to @PieterGoosen for providing the first version.

http://pastebin/xKBepGQz

<?php
$args = array(
    'post_type' => 'produkty', // your CTP name
    'meta_key' => 'produkt_nowosc', // custom meta key for displaying, in my case: the newest product in offer
    'meta_value' => 'tak', // field above set to 'yes'
    'posts_per_page' => -1
);

$query = new WP_Query($args);  
$q = array();

while ( $query->have_posts() ) {
    $query->the_post();
    // displaying the content of product box etc.
    $a = '<a href="'. get_permalink() .'">' . get_the_title() .'</a>';
    $terms = wp_get_post_terms( $post->ID, 'kategorie_produktow', $args );
    foreach ( $terms as $term) {
        $term_link = get_term_link( $term );
        $b = '<span>'.$term->name.'</span>';    
    }
    $q[$b][] = $a;
}
wp_reset_postdata();
foreach ($q as $key=>$values) {
    echo $key;
    echo '<ul>';
        foreach ($values as $value){
            echo '<li>' . $value . '</li>';
        }
    echo '</ul>';
}
?>
Post a comment

comment list (0)

  1. No comments so far