• Resolved GradyD

    (@adroidman)


    I have created a custom taxonomy, app_category, which works perfectly. I am working on a widget to get posts by the category they are in.

    // widget form creation
      function form($instance) {
      	// Check values
      	$category = isset( $instance['category'] ) ? (int) $instance['category'] : '';
      	$terms = get_terms( 'app_category', 'parent=0&hide_empty=0' );
      	if ( !empty( $terms ) && !is_wp_error( $terms ) ) {
      ?>
          <!-- Category Select Menu -->
          <p>
            <label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php _e( 'App Category: ', 'AppManager_plugin' ); ?></label>
            <h1><?php echo $category ?></h1>
            <select id="<?php echo $this->get_field_id( 'category' ); ?>" name="<?php echo $this->get_field_name( 'category' ); ?>" >
              <?php foreach ( $terms as $term ) { ?>
                <option <?php selected( $category, $term->term_id ); ?> value="<?php echo $term->term_id; ?>"><?php echo $term->name; ?></option>
              <?php } ?>
            </select>
          </p>
      <?php
      	}
      }
    
        // display widget
        function widget($args, $instance) {
           extract( $args );
           // these are the widget options
           $category = $instance['category'];
           echo $before_widget;
    
           // Query for data
            $args = array(
              'post_type'       => 'apps_post',
              /** Category slug, needs to be dynamic from widget selection **/
              'app_category'    => $category
            );
            $Apps = new WP_Query( $args );
            if( $Apps -> have_posts() ) {
              while( $Apps -> have_posts() ) {
                $Apps -> the_post();
                $postid = get_the_ID();
                ?>
                  <h1><?php the_title() ?></h1>
                  <div class='content'>
                    <?php the_excerpt() ?>
                    <?php
                      $value = get_post_meta( $postid, 'apps_post', true );
                      echo '<h3>Meta Data: ' . $value . '</h3>';
                    ?>
                  </div>
                <?php
              }
            }

    I am able to output $category and it is the same ID that I am seeing when I print $category in the widget box. The problem is that I am not able to get any of the posts back. I just get the number. I know the issue is with my $args and using 'app_category' but I am not sure what to change it to so that I can get all the posts in a custom taxonomy using the category ID, can anyone help?

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WP_Query custom taxonomy by category ID’ is closed to new replies.