WordPress.org

Ready to get started?Download WordPress

Forums

"post_type" is ignored when I include "tag__in" in my get_posts query (3 posts)

  1. ramblinollie
    Member
    Posted 1 year ago #

    "post_type" is ignored when I include "tag__in" in my "get_posts" query.

    E.g. This query returns posts AND pages.

    $post_type_array = array('post');
    $tag_id_array = array(23, 45);
    
    $custom_links_args = array(
        'post_type' => $post_type_array,
        'tag__in' => $tag_id_array
    );
    
    $custom_links_array = get_posts($custom_links_args);

    Has anyone else had this problem? I'm beginning to think it's a WordPress bug.

  2. Dunc
    Member
    Posted 1 year ago #

    I'm having this problem too. Did you find a resolution to this?

    My code is a bit more complex but the effect is the same. I've tried get_posts and WP_Query loops to no avail.

    // List posts from a custom post type, that share tags with the current post
      global $post;
      $tags = get_the_tags(); // get an array of all the tags as objects.
      $tag_array = array();
      foreach( $tags as $tag ) {
        $tag_array[] = $tag->term_id; // save the id in an array
      }
      $tag_string = implode(",",$tag_array);
      $myargs = array(
    	'post_type' => 'brand',
    	'tag__in' => array($tag_string),
        'showposts'=>-1,
        'caller_get_posts'=>1
        );
            $brands = get_posts( $myargs );
    
            // The Loop
            foreach ($brands as $post) : setup_postdata($post);
                echo '<p>';
                    the_title();
                echo '</p>';
            endforeach;
    
            // Reset Post Data
                wp_reset_postdata();
  3. ramblinollie
    Member
    Posted 1 year ago #

    I forget what project it was that I needed this for, sorry! The following query looks similar to the one I posted here. I'm not a WP ninja so I probably can't be much more help than this...

    $custom_links_args =  array(
        'post_type'  => $post_type_array,
        'exclude' => $post_id,
        'posts_per_page' => $display_num,
        'tax_query' => array( array(
          'taxonomy' => 'post_tag',
          'terms'    => $page_tags_array,
        ) )
      );

Topic Closed

This topic has been closed to new replies.

About this Topic