WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Querying posts by taxonomy (8 posts)

  1. Topher
    Member
    Posted 3 years ago #

    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?

  2. chinmoy
    Member
    Posted 3 years ago #

    $args = array('post_type' => texonomy_name,
        'posts_per_page'=>'10'
    );
  3. Topher
    Member
    Posted 3 years ago #

    @chinmoy29 that doesn't work at all.

  4. chinmoy
    Member
    Posted 3 years ago #

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

  5. Topher
    Member
    Posted 3 years ago #

    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?

  6. chinmoy
    Member
    Posted 3 years ago #

    ok. now write this

    $args = array(
        'misc'=>'Banner', // or 'misc'=>'banner'
        'post_type'=>'post',
        'posts_per_page'=>'10'
    );
  7. Topher
    Member
    Posted 3 years ago #

    I just tried that, it still doesn't work. :(

  8. Topher
    Member
    Posted 3 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic