0
votes

What I'm trying to do is generate a new Custom Post Type called report, add a taxonomy, query the reports, and then generate a shortcode in the format of [report_listing category="general"] to display the reports in the category General.

Right now, on report/index.twig, I get a listing of all reports using the shortcode [report_listing], and the error No reports available when using [report_listing category="general"].

I am using Advanced Custom Fields, but that doesn't appear to be an issue with this or the rest of the site. The categories are being generated and associated with the posts, since this is a sample {{ dump }} output on /index.twig for one report when using [report_listing] and all reports are displayed:

["report_category"]=> array(1) { [0]=> object(Timber\Term)#2131 (15)
{ ["PostClass"]=> string(11) "Timber\Post" ["TermClass"]=> string(4) "Term"
["object_type"]=> string(4) "term" ["_children"]=> NULL ["name"]=> string(7)
"General" ["taxonomy"]=> string(15) "report_category" ["id"]=> int(89) ["ID"]=> int(89)
["term_id"]=> int(89) ["slug"]=> string(7) "general" ["term_group"]=> int(0)
["term_taxonomy_id"]=> int(89) ["parent"]=> int(0) ["count"]=> int(0) ["filter"]=> string(3) "raw" } } 

I looked at ACF Querying custom post types with taxonomies , but I am using tax_query parameters. There are no php errors on the debug log.

The loop on index.twig:

{% if reports %}

    {% for report in reports %}

        (html)

    {% endfor %}

    {% else %}

        No reports available.

{% endif %}

Full code in the plugin that generates the custom post type and generates the shortcode:

// Add a standard Custom Post Type called report

function add_report_post_type() {
    register_post_type('report',
    array(
        'labels' => array(
            'name' => __('Reports'),
            'singular_name' => __('Report'),
            'add_new' => __('Add New'),
            'add_new_item' => __('Add Report'),
            'edit' => __('Edit'),
            'edit_item' => __('Edit Report'),
            'new_item' => __('New Report'),
            'view' => __('View Report'),
            'view_item' => __('View Report'),
            'search_items' => __('Search Reports'),
            'not_found' => __('No reports found'),
            'not_found_in_trash' => __('No reports found in Trash')
        ),
        'public' => true,
        'hierarchical' => true,
        'has_archive' => false,
        'supports' => array(
            'title',
            'revisions'
        ),
        'can_export' => true,
        'menu_position' => 5,
        'menu_icon' => 'dashicons-clipboard',
        'rewrite' => array(
            'slug' => 'report',
            'with_front' => false,
            'hierarchical' => true
        ),
        )
    );
}
add_action('init', 'add_report_post_type');


// Add categories

function reports_taxonomy() {
    $labels = array(
        'name'                       => _x( 'Report Categories', 'Taxonomy General Name', 'text_domain' ),
        'singular_name'              => _x( 'Report Categories', 'Taxonomy Singular Name', 'text_domain' ),
        'menu_name'                  => __( 'Categories', 'text_domain' ),
        'all_items'                  => __( 'All Items', 'text_domain' ),
        'parent_item'                => __( 'Parent Item', 'text_domain' ),
        'parent_item_colon'          => __( 'Parent Item:', 'text_domain' ),
        'new_item_name'              => __( 'New Item Name', 'text_domain' ),
        'add_new_item'               => __( 'Add New Item', 'text_domain' ),
        'edit_item'                  => __( 'Edit Item', 'text_domain' ),
        'update_item'                => __( 'Update Item', 'text_domain' ),
        'view_item'                  => __( 'View Item', 'text_domain' ),
        'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
        'add_or_remove_items'        => __( 'Add or remove items', 'text_domain' ),
        'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
        'popular_items'              => __( 'Popular Items', 'text_domain' ),
        'search_items'               => __( 'Search Items', 'text_domain' ),
        'not_found'                  => __( 'Not Found', 'text_domain' ),
        'no_terms'                   => __( 'No items', 'text_domain' ),
        'items_list'                 => __( 'Items list', 'text_domain' ),
        'items_list_navigation'      => __( 'Items list navigation', 'text_domain' ),
    );
}


function create_report_taxonomy () {
register_taxonomy(
        'report_category', 'report',
        array(
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'meta_box_cb'                   => false,
            )
    );
}
add_action( 'init', 'create_report_taxonomy', 2 );



// Query all reports and create shortcode

function report_get_listing($params) {
    $reports = [];
    $context = Timber::get_context();

    $args = array(
        'post_type' => 'report',
        'orderby' => [
            'weight' => 'ASC',
            'post_date' => 'DESC'
        ],
        'post_status' => 'publish',
        'meta_query' => [
            'weight' => [
                'key'     => 'weight',
                'compare' => 'EXISTS',
                'type'    => 'NUMERIC'
            ],

        ],
    );


// Check for a report category in the query

  if (!empty($params['category'])) {
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'report_category',
                'terms' => explode(',', $params['category']),
                'field' => 'slug',
                'operator' => 'IN',
            ),
        );
    }

    query_posts($args);

    $report_listing = Timber::get_posts($args);

    foreach ($report_listing as $p) {
        $reports[] = get_single_report($p);
    }

    $context['reports'] = $reports;

    return Timber::compile('report/index.twig', $context);
}


// Build all the reports for the shortcode

function get_single_report($post) {
    $report = [
        'id' => $post->ID,
        'link' => $post->link,
        'title' => $post->title(),
        'date' => $post->post_date,
        'summary' => $post->get_field('summary'),
        'report_category' => $post->get_field('report_category'),
        'document' => $post->get_field('document'),
        'image' => $post->get_field('image'),
        'url' => $post->get_field('url')
    ];
    return $report;
}
add_shortcode('report_listing', 'report_get_listing');
2
Tested your code on a clean install and it is working fine (I had to create a simple for loop in the Twig template though, as you did not provide it). There must be something else interfering with your code :/Víctor
Thanks, can you post the for loop you used on index.twig?BlueDogRanch
Your index.twig should not be at fault, as it is working when you don's set a category in your shortcode (according to your original post). The code I used was a simple: {% for r in reports %}{{ r.title }}{% endfor %}Víctor
Could it be that when you created the custom field with ACF for weight you made a typo and thats why it is not showing... wait, that can't be, because then the shortcode would not show anything at all even without the category filter. That's why I think it has to do with something else in your code I can't see here. Maybe try simplifying your query removing the orderby, meta_query from $argsVíctor
This may sound stupid.. but, are you sure you're using the correct category slug? And that the category actually contains published posts having the weight metadata? Because (although I commented out the query_posts($args);) your code works fine for me, both with and without specifying any category slugs. So it's possible that a plugin or custom code is filtering the query - try disabling all plugins except Timber of course. And btw, add_action('init', 'add_report_post_type'); is duplicated in your code.. add_report_post_type() is hooked twice to init.Sally CJ

2 Answers

2
votes

The issue turned out to be a weird plugin conflict :( . I found that using a query monitor. The above example code in my question works fine.

0
votes

From reading your current question and code, I can see a few issues. But I'd like to address the shortcode function which is what I think is causing the reports to not get listed by category.

// Query all reports and create shortcode

function report_get_listing($params) {
    $reports = [];

   $a = shortcode_atts( array(
    'category' => '', // [report_listing category=""]
   ), $params );

    $context = Timber::get_context();

    $args = array(
        'post_type' => 'report',
        'orderby' => [
            'weight' => 'ASC',
            'post_date' => 'DESC'
        ],
        'post_status' => 'publish',
        'meta_query' => [
            'weight' => [
                'key'     => 'weight',
                'compare' => 'EXISTS',
                'type'    => 'NUMERIC'
            ],

        ],
    );


  // Check for a report category in the query

  if (!empty($a['category'])) {
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'report_category',
                'terms' => explode(',', $a['category']),
                'field' => 'slug',
                'operator' => 'IN',
            ),
        );
    }

    query_posts($args);

    $report_listing = Timber::get_posts($args);

    foreach ($report_listing as $p) {
        $reports[] = get_single_report($p);
    }

    $context['reports'] = $reports;

    return Timber::compile('report/index.twig', $context);
}

add_shortcode('report_listing', 'report_get_listing');