category__in
, or those with the same tag(s), using tag__in
. Is it possible to put of them into one big mixing pot, if you will, and select posts from all of them? I don’t mean those that have the same category(ies) AND the same tag(s), but those that have either or.
]]>$tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
$args = array(
'posts_per_page' => 4,
'cat' => 5,
'post__not_in'=> array($post->ID),
'tag__in'=> $tag_ids
);
$query = new WP_Query($args);
$nbposts = $query->post_count;
$ids = array($post->ID);
while ( $query->have_posts() ) : $query->the_post();
$ids[] = get_the_ID();
... my post content
endwhile; wp_reset_postdata();
if($nbposts < 4){
$nbpost = 4 - $nbposts;
$args['posts_per_page'] = $nbpost;
$args['post__not_in'] = $ids;
$args['tag__in'] = '';
$query = new WP_Query($args);
while ( $query->have_posts() ) : $query->the_post();
... my post content
endwhile; wp_reset_postdata();
}
]]>$query = array('cat' => '4', 'orderby' => 'rand', 'tag__in' => array(7,9))
This works great.
The problem is that I can’t seem to generate the array for tag__in dynamically. If I use an array of values ( ‘tag__in’ => $array )it always outputs thus:
[tag__and] => Array ( [0] => 7,9 )
But the query wants only values separated by commas. Because of this it only recognizes the first tag.
So how do I make an array only output comma separated values without keys? (PS I tried implode() and got the same result).
Thanks!
]]>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.
]]>Here’s the code I’ve got:
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } elseif ( get_query_var('page') ) { $paged = get_query_var('page'); } else { $paged = 1; }
$allowedtags = array ( 'video', 'videos', 'photo', 'photos' ) ;
$args = array(
'post_type' => 'portfolio',
'paged' => $paged,
'meta_key' => 'portfolio-image',
'posts_per_page' => -1
);
$args = array_merge ( $args, array( 'tag__in' => $allowedtags));
query_posts( $args );
rewind_posts();
?>
However no posts are returned, even though I’m using the correct tags!
Does anyone have any suggestions as to why it’s not working?
Thanks
You can see what I mean here:
https://www.jordans-town.com/jsearch?ppall=both&tefull=excerpts&ppp=10&eng=1&paged=1&search_titles=1&search_content=1&search_excerpts=1&search_custom=1&selected_cat_parent=-1&tag_any=1012,1223
If you go to the category selector and choose “Defenders” and add it to the “ALL” section, it should still show some results, but shows none.
]]>I am trying to retrieve all posts from tag ID 6 in category ID 3.
For some reason, the below code only works with the combinations:
-category__in & tag__and
-category__and & tag__in
But it doesn’t work with category__in & tag__in:
$args=array(
‘category__in’ => array(3), ‘tag__in’ => array(6),
‘post__not_in’ => array($post->ID),
‘showposts’=>3,
‘caller_get_posts’=>1
);
$my_query = new WP_Query($args);
Please, can someone explain?
]]>I have a blog that hosts only videos and I want to relate them to each other via tags.
So there is this section that pulls thumbnails from attached images of another post in order to offer up other clips which looks like this:
<?php $my_query = new WP_Query('tag__in=tag3&orderby=rand&cat=6&showposts=3'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); if( $post->ID == $current ) continue; ?>
<a href="<?php the_permalink(); ?>"><?php $images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
$firstImageSrc = wp_get_attachment_image_src(array_shift(array_keys($images)));
echo "<img src=\"{$firstImageSrc[0]}\" width=\"50\" height=\"50\" />"; ?></a>
<?php endwhile;?>
Now at the moment the tag is ‘tag3’ which was a test tag and doesn’t actually exist anymore. The function runs nevertheless but the pattern is neither random nor can I deduce any logic from it.
What I would love to do is to put all the posttags in an array and pass that on to the ‘tag__in’, but I’m not sure if that can even read multiple tags, and my PHP basics are pretty bad so I’ve been playing with it rather unsuccessfully even after googling my way around on how to pass arrays or checking out the parameter s in the codex.
Any help would be greatly appreciated.
Thanks,
Czymra