Forums

Querying custom taxonomies in the custom post type using query posts (5 posts)

  1. guru4ever
    Member
    Posted 5 months ago #

    Guys,
    am playing with custom post types and custom taxonomies . i have registered the custom post type. and custom taxonomies also added. i want to query the custom post using the categories name i have added. when am using the following query it displays all the posts.

    <?php query_posts('post_type=>sample_content&category_name=>bannertop');?>.
    i need help on how to query the particular custom posts using custom category . and i want to know any other methods are available to query particular posts(custom)?..........

    Thanks in advance
    Guru4ever

  2. keesiemeijer
    moderator
    Posted 5 months ago #

    What is the name of the custom taxonomy you registered?

    Try it with a "tax_query": https://codex.wordpress.org/Function_Reference/WP_Query#Taxonomy_Parameters

    example [untested]:

    <?php
    $args = array(
      'post_type'   => 'sample_content',
      'tax_query' => array(
    		array(
    			'taxonomy' => 'taxonomy_name',
    			'field' => 'slug',
    			'terms' => 'bannertop'
    		)
    	)
    );
    query_posts($args);
    ?>

    Change "taxonomy_name" to the taxonomy name you registered.

    Also read how to pass parameters to functions: https://codex.wordpress.org/How_to_Pass_Tag_Parameters#Tags_with_query-string-style_parameters

  3. guru4ever
    Member
    Posted 5 months ago #

    Hi keesiemeijer,
    thanks for the reply , i have tried what you suggest it works.but it displays all the posts in that category.i want to display particular posts under the category bannertop. post slug name is "calendar". help needed.

    <?php
    $args = array(
      'post_type'   => 'sample_content',
      'tax_query' => array(
    		array(
    			'taxonomy' => 'site-categories',
    			'field' => 'slug',
    			'terms' => 'bannertop'
    		)
    	)
    );
    query_posts($args);
    ?>
    <?php?>
    <?php while ( have_posts() ) : the_post(); ?>
    <h3><?php the_title()?></h3>
    <?php the_content()?>
    <?php endwhile; wp_reset_query(); ?>

    Thanks in advance---
    Guru4ever

  4. keesiemeijer
    moderator
    Posted 4 months ago #

    If you want to query a specific post you can just use:

    <?php query_posts('post_type=sample_content&name=calendar'); ?>

    or

    <?php
    $args = array(
      'post_type'   => 'sample_content',
      'name' => 'calendar',
      'tax_query' => array(
    		array(
    			'taxonomy' => 'site-categories',
    			'field' => 'slug',
    			'terms' => 'bannertop'
    		)
    	)
    );
    query_posts($args);
    ?>

    https://codex.wordpress.org/Function_Reference/WP_Query#Post_.26_Page_Parameters

  5. guru4ever
    Member
    Posted 4 months ago #

    hi keesiemeijer,

    Thanks. now i got it.. am having one more question. am using custom post type . in that am given capability-type as page . what is the best practice whether to use post or page in custom post-type capability.need help

    Regards,
    Guru4ever

Reply

You must log in to post.

About this Topic