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

  1. ramblinollie
    Posted 3 years 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
    Posted 3 years 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),
            $brands = get_posts( $myargs );
            // The Loop
            foreach ($brands as $post) : setup_postdata($post);
                echo '<p>';
                echo '</p>';
            // Reset Post Data
  3. ramblinollie
    Posted 3 years 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