1

I am trying to create a plugin, I need a custom post and taxonomy. But it can be accessible in admin side, but the taxonomy is not working on front-end.

This is how I registered the custom post and taxonomy:

function post_type_questionnaire()
{
    $labels = array(
        'name' => _x('Questionnaire', 'post type general name'),
        'singular_name' => _x('Questionnaire', 'post type singular name'),
        'add_new' => _x('Add New Question', 'questionnaire'),
        'add_new_item' => __('Add New Questionnaire')
    );

     $args = array(
        'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => true,
        '_builtin' => false, // It's a custom post type, not built in!
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => null,
        'supports' => array('title',
                            //'editor',
                            /*'excerpt',
                            'thumbnail',
                            'trackbacks',
                            'custom-fields',
                            'comments',
                            'revisions',
                            'author',
                            'page-attributes'*/
    ));

    register_post_type('questionnaire',$args);

}
add_action('init', 'post_type_questionnaire');



function create_questionnaire_taxanomies(){
    register_taxonomy('qcategories','questionnaire', array(
        'hierarchical'=>true,
        'label'=>'Questionnaire Categories',
        'rewrite' => array( 'slug' => 'questionnaire' )
    ));
}

add_action('init', 'create_questionnaire_taxanomies',0);

I am using a shortcode to display it in front-end.

by using the code below it shows all posts

 $args = array(
    'post_type' => 'questionnaire',
    'posts_per_page' => -1,
 );
 $query = new WP_Query($args);
 if ( $query->have_posts() ) { ?>
 <?php while ( $query->have_posts() ) :query->the_post(); ?>

But when I try to specify a taxonomy term it doesn't work and this is the code:

  $args = array(
    'post_type' => 'questionnaire',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'qcategories',
            'field'    => 'slug',
            'terms'     => $atts["name"]
        )
    )
);
$query = new WP_Query($args);
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>

When I tried to display the count of post in a template page it show nothing:

 $term = get_term( 3, 'qcategories' );
 echo $term->count;

This outputs nothing, so I believe that the taxonomy is not registering, can anybody help me to register the taxonomy in my custom plugin. Thanks in advance!

0

You might need to resave your permalinks. Just press the Save button on the permalink settings: /wp-admin/options-permalink.php

Otherwise, your code works after cleaning up just a couple of typos:

function post_type_questionnaire()
{
    $labels = array(
        'name' => _x('Questionnaire', 'post type general name'),
        'singular_name' => _x('Questionnaire', 'post type singular name'),
        'add_new' => _x('Add New Question', 'questionnaire'),
        'add_new_item' => __('Add New Questionnaire')
    );

     $args = array(
            'labels' => $labels,
            'public' => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'query_var' => true,
            'rewrite' => false,
            '_builtin' => false, // It's a custom post type, not built in!
            'capability_type' => 'post',
            'hierarchical' => false,
            'menu_position' => null,
            'supports' => array(
                'title',
                //'editor',
                /*'excerpt',
                'thumbnail',
                'trackbacks',
                'custom-fields',
                'comments',
                'revisions',
                'author',
                'page-attributes'*/
    ));

    register_post_type('questionnaire',$args);

}
add_action('init', 'post_type_questionnaire');



function create_questionnaire_taxanomies(){
    register_taxonomy('qcategories','questionnaire', array(
        'hierarchical'=>true,
        'label'=>'Questionnaire Categories',
        'rewrite' => array( 'slug' => 'questionnaire' )
    ));
}

add_action('init', 'create_questionnaire_taxanomies',0);

This template code had a few typos, but it would have thrown an error:

$atts["name"] = 'stackexchange';

$args = array(
    'post_type' => 'questionnaire',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'qcategories',
            'field'    => 'slug',
            'terms'     => $atts["name"]
        )
    )
 );
 $query = new WP_Query($args);
 if ( $query->have_posts() ) { ?>
 <?php while ( $query->have_posts() ) : $query->the_post(); ?>

<?php the_title(); ?>

 <?php endwhile; ?>

 <?php } ?>
  • that doesn't help – shellysaju Feb 17 '17 at 14:28
  • Just to be sure, double check that the term slugs match. Make sure that what you have in the post is the same you have in the query atts. – stillatmylinux Feb 17 '17 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.