• Resolved Giorgio25b

    (@giorgio25b)


    Hi all,
    I don’t seem to be able to filter a query for CPT with custom taxonomy; I have a CPT called ‘publication’ and a CT called ‘publication-types’; under ‘publication-types’ I created some categories and one of those is called ‘reports’.
    I’m trying to filter in a custom loop 2 posts out of ‘reports’ only: http://www.firstcall.workingdesign.ca/publication-types/reports/ and in the future I will try to have 2 loops filtering also another category called ‘letters’, but for the sake of the question let’s consider only ‘reports’.

    Here is the code I’m trying to put in my page template:

    <!-- custom loop 1 -->
                            <?php
                                add_filter('tc_show_post_metas', '__return_true');
                                global $wp_query, $wp_the_query;
    
                                $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    
                                $wp_query = new WP_Query( array(
                                    'paged'             => get_query_var('paged') ? get_query_var('paged') : 1,
                                    'post_type'         => 'publication',
                                    'publication-types' => $term->taxonomy,
                                    'reports'           => $term->slug,
                                    'post_status'       => 'publish',
                                    'posts_per_page'    => 2,   //show n posts
                                    //'cat'               => 77, //include this category from the posts list
                                    //'tag_id'            => '77',
                                    //'tag'               => 'reports',
                                    //'page_name'         => 'publication-s',
                                    //'taxonomy'          => 'publication-types',
                                    //'category_name'     => 'reports',
                                    //'page_name'         => 'publication-types/reports',
                                    //others parameters here: http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
                                ) );
                            ?>
    
    <!-- Customizr Loop here -->
    
    <!-- reset the main query -->
    <?php $wp_query = $wp_the_query; ?>

    well, it does not work because I’m missing the point, it outputs only the 2 most recent posts under ‘publication’, what am I missing && || doing wrong?
    https://github.com/giorgioriccardi/filtering-cpt-and-ct-in-WP-customizr-theme/blob/master/publications-page.php#L58

    Thanks for any tip you can give here 😉

Viewing 1 replies (of 1 total)
  • Thread Starter Giorgio25b

    (@giorgio25b)

    SOLUTION is related to the missing 'tax_query' => array(...)
    https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

    this is a working query that filters CPT, CT and related category:

    <!-- custom loop 1 -->
                            <?php
                                add_filter('tc_show_post_metas', '__return_true'); //this applies only to Customizr theme
    
                                global $wp_query, $wp_the_query;
    
                                $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    
                                $wp_query = new WP_Query( array(
                                    'paged'             => get_query_var('paged') ? get_query_var('paged') : 1,
                                    'post_type'         => 'publication',
                                    'tax_query' => array(   //https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
                                                         array(
                                                            'taxonomy' => 'publication-types',
                                                            'field'    => 'slug',
                                                            'terms'    => 'reports',
                                                            )
                                                        ),
                                    'post_status'       => 'publish',
                                    'posts_per_page'    => 2,   //show n posts
                                    //'cat'               => 77, //include this category from the posts list
                                    //others parameters here: http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
                                ) );
                            ?>

    here is the full page:
    https://github.com/giorgioriccardi/filtering-cpt-and-ct-in-WP-customizr-theme/blob/master/publications-page.php#L64

Viewing 1 replies (of 1 total)

The topic ‘Filtering CPT and CT in a custom loop’ is closed to new replies.