WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] WordPress query using term id rather than name (4 posts)

  1. robmuzo
    Member
    Posted 2 years ago #

    Can anybody tell me how to change the query below so that it works of the term id rather than the term name?

    <?php
        query_posts(array(
            'post_type' => 'products',
    		'taxonomy' => 'category',
    		'term' => 'mytermname',
            'showposts' => 10
        ) );
        ?>

    Something like this: (Does not work)

    <?php
        query_posts(array(
            'post_type' => 'products',
    		'taxonomy' => 'category',
    		'term_id' => '57',
            'showposts' => 10
        ) );
        ?>

    Thanks

  2. bythegram
    Member
    Posted 2 years ago #

    Untested but this might work:

    <?php
         $args = array(
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'category',
    			'field' => 'id',
    			'terms' => 57
    		)
    	)
    );
    $query = new WP_Query( $args ); ?>

    Resource: here

  3. robmuzo
    Member
    Posted 2 years ago #

    Managed to get it working with this:

    <?php $termID = get_post_meta($post->ID, 'related_category_id_meta', true); ?>
    <?php query_posts( 'post_type=products&cat='.$termID.'&posts_per_page=-1' ); ?>

    Basically I have a dropdown category selector on the custom post type category page which allows the user to select the related categories to feed under the category page

  4. robmuzo
    Member
    Posted 2 years ago #

    Resolved

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags