WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Querying posts by taxonomy

[Resolved] Querying posts by taxonomy

  • Topher

    @topher1kenobe

    I have a custom taxonomy set up for regular posts, called Miscellaneous. I want to use that in WP_Query to get all posts that have Banner as one of the values of that taxonomy. I have this code:

    $args = array(
        'Miscellaneous'=>'Banner',
        'post_type'=>'post',
        'posts_per_page'=>'10'
    );
    
    $banners = null;
    $banners = new WP_Query($args);
    while($banners->have_posts()) : $banners->the_post();?>

    I’m getting all posts though. I did a print_r on $banners and the sql going to mysql it this:
    SELECT SQL_CALC_FOUND_ROWS fh_posts.* FROM fh_posts WHERE 1=1 AND fh_posts.post_type = 'post' AND (fh_posts.post_status = 'publish') ORDER BY fh_posts.post_date DESC LIMIT 0, 10

    I don’t see anything about my taxonomy in there at all. Is it possible to filter by taxonomy at all?

Viewing 7 replies - 1 through 7 (of 7 total)
  • $args = array('post_type' => texonomy_name,
        'posts_per_page'=>'10'
    );
    Topher

    @topher1kenobe

    @chinmoy29 that doesn’t work at all.

    how to create custom taxonomy? can u give that code?

    Topher

    @topher1kenobe

    I have this in functions.php:

    add_action( 'init', 'fh_taxonomies', 0 );
    function fh_taxonomies() {
        register_taxonomy(
            'misc',
            'post',
            array(
                'hierarchical' => 'true',
                'label' => 'Miscellaneous',
                'query_var' => 'true',
                'rewrite' => 'false'
            )
        );
    }

    I see there I have ‘misc’, should I perhaps be using that instead?

    ok. now write this

    $args = array(
        'misc'=>'Banner', // or 'misc'=>'banner'
        'post_type'=>'post',
        'posts_per_page'=>'10'
    );
    Topher

    @topher1kenobe

    I just tried that, it still doesn’t work. 🙁

    Topher

    @topher1kenobe

    I figured it out. In my register_taxonomy, for query_var I had ‘true’ which is a string, not a boolean setting. I changed it to true and it works!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Resolved] Querying posts by taxonomy’ is closed to new replies.