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

admin2025-06-07  25

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>';
}
?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749253316a317638.html

最新回复(0)