I'm a bit of a wordpress newbie and was trying to find good solution what I was trying to do. Essentially I to get a list of categories that have a specific tag 'detination'. Once I do this I want to query for all posts with the above filter as well.
So What I have now is something like this:
$destCategories = get_categories( array('tag' => 'destination' , 'exclude' => '1') );
$posts = get_posts($destCategories);
However, get_categories returns an Array of category specific information which can't really be used as a filter for get_posts. Does anyone have suggestions on how can solve this? I could essentially manually iterate through the destCategories array and construct a string of all the category names and use that as a filter in get posts, but I wanted to know if there's a more elegant solution that might be available.
Thanks for your help!
There's no tag argument for get_categories() : http://codex.wordpress.org/Function_Reference/get_categories#Parameters
I guess you could try passing in a taxonomy array, I don't know how it's gonna work out though.
If you want to get posts by tag, you could just use get_posts() : http://codex.wordpress.org/Template_Tags/get_posts#Taxonomy_Parameters
You could try something like this :
$args = array(
'post_type' => 'post',
'tag' => 'destination',
);
$query = new WP_Query( $args );
if ($query->have_posts()):
while ($query->have_posts()): $query->the_post(); ?>
<?php the_title(); ?>
<?php
endwhile;
endif;
?>
Ok so i ended up with something like this, which seems to work:
$query = get_posts( $myfilter );
$destCategories = get_categories( array('tag' => 'destination' , 'exclude' => '1') );
?>
<div id="main">
<div id="content clearfix">
<p>this is using front-page.php</p>
<?php //echo var_dump($destCategories) ?>
<?php
if ( have_posts( $myfilter ) ) {
foreach ( $destCategories as $category ) :
$posts = get_posts( array('category_name' => $category->slug) );
foreach ( $posts as $post ) :
?>
<h3> <?php echo the_title(); ?> </h3>
<?php
endforeach;
wp_reset_postdata();
//the_content();
endforeach;
?>
Related
This not working out for me. Any tips or example to make it work? post args = array($tags) didnt get value correctly.
<h6>seal</h6>
<h6>mark</h6>
<?php
$tags = "<script>document.write(h6);</script>";
$args = array(
'post_type' => 'Rune',
'posts_per_page' => -1,
'tag' => $tags,
);
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ) :
setup_postdata( $post ); ?>
<?php the_content(); ?>
<?php endforeach;
wp_reset_postdata();
?>
$tags is going to equal the string <script>document.write(h6);</script>. PHP isn't going to evaluate your JS.
The result you're seeing is correct under the circumstances. You need to change the way you're getting tags in order to pass them in to your $args array.
Without knowing more about the problem I can't suggest a better approach to take.
I have a wp_query getting all posts in a category with the same slug as the title as the current page.
Where i have become stuck is modifying wp_query category_name => $post->post_name plus a text string.
For example if page was to be called "Long Day" all posts with the category slug "long-day" will be shown. I need to bring in these posts in one loop and in another have post_name plus the text sting eg long-day-today.
This is what i have so far...
<?php
global
$post; $args = array(
'category_name' => $post->post_name
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="entry"><?php the_content(); ?></div>
<?php echo $cat ?>
<?php endwhile; else: ?>
<p>Sorry, there are no posts to display</p>
<?php endif; ?>
[EDIT]
I feel like a bit of a fool now, after a little playing i came up with this
global
$post;
$exclude = "hello";
$args = array(
'category_name' => "{$post->post_name} . $exclude"
);
Which seems to do the trick, if there is a better way of achieving this i would still be interested to hear...
Thanks
global
$post;
$exclude = "hello";
$args = array(
'category_name' => "{$post->post_name} . $exclude"
);
How do I change product in same category randomly? I've been looking but can't seem to find any plugin/script to do this, anyone got an idea on this... thanks
You can use the following code to display the products rows of "CATXXX" :
<?php echo do_shortcode( '[product_category category="CATXXX" per_page="8" columns="4" orderby="rand"]' ) ?>
Also you can treat products like any other post type and query using get_posts(). Limit the number of posts retrieved to 1 and change the orderby parameter to random:
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'category' => 'CATXXX'
'post_type' => 'product');
$random_products = get_posts( $args );
foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();
You can also use new WP_Query() which would be similar.
You can try this code shortcode
[product_category category="category_slug" per_page="10" orderby="rand"]
Or
<?php echo do_shortcode( '[product_category category="category_slug" per_page="10" orderby="rand"]' ) ?>
I'm using wordpress with custom post type UI plugin and ACF plugin.
Trying to build a “single” template with multiple feeds of custom post types by custom custom taxonomy. Using this code, with a few variations to figure out what am i doing wrong.
Got 2 pieces of code like this in a row
<?php if( get_field('collectiona') ):
$argsc = array(
'post_type' => 'products',
'product-collections' => get_field('collectiona'),
);
$prods2 = new WP_Query( $argsc );
if( $prods2->have_posts() ) {
while( $prods2->have_posts() ) {
$prods2->the_post();
?>
Whatever post code
<?php
}
}
else {
echo '';
}
?>
<?php endif; ?>
collectiona is a taxonomy field. With the piece of code, shown above, it just shows all the “products” posts out there. I’ve also tried using a text field with taxonomy slug. It shows first feed perfectly fine, if i’m not using first if statement (<?php if( get_field(‘collectiona’) ): ?>), and if that statement is present- same thing happens. All the “products” are shown. However, even with first feed shown fine, 2nd feed still shows all the “products” out there. Despite what taxonomy slug says.
I’m trying to build it the way, admin could chose a dropdown taxonomy. Text field with taxonomy slug is just an example.
p.s.
I use term object
Full template code is here jsfiddle.net/pudfbxhv . I know jsfiddle is useless for wp templates, but that's a pretty big piece of code
EDIT
Here is updated code
<?php
$taxterms = get_field("collectiona"); ?>
<?php
$args = array(
'post_type' => 'products',
'tax_query' => array(
array(
'taxonomy' => 'product-collections',
'field' => 'id',
'terms' => $taxterm->term_id
)
)
);
$myquery = new WP_Query( $args );
if($myquery->have_posts()) : ?>
<ul>
<?php while ( $myquery->have_posts() ) : $myquery->the_post(); ?>
<li> <img src="<?php the_field('prod_featured_image'); ?>" onmouseover="this.src='<?php the_field('prod_hover_featured_image'); ?>'" onmouseout="this.src='<?php the_field('prod_featured_image'); ?>'" />
<h2><?php the_field('prod_subtitle'); ?></h2>
<p>$<?php the_field('prod_price'); ?></p>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); ?>
Well, that may be a perversion, but it worked for me.
$termss = get_field('collectiona');
$slll = $termss->slug;
$args = array(
'post_type' => 'products',
'product-collections' => $slll,
);
$lineblocks = new WP_Query( $args );
if( $lineblocks->have_posts() ) {
while( $lineblocks->have_posts() ) {
$lineblocks->the_post();
Also, gotta remember to put the following code after every array
<?php wp_reset_query(); ?>
I am just playing around with WordPress and neither I have any idea abt PHP.
I am trying to fetch few random posts using get_posts() function of the WordPress
my code is something like this
<?php
args1 = array( 'numberposts' => 12 ,'orderby' => 'rand');
$rand_posts1 = get_posts( $args1);
foreach( $rand_posts1 as $randpost1 ) : ?>
<?php the_title(); ?>
<?php endforeach; ?>
But this code is only returning same post all the 12 times and that is the lastest post.
I am clueless what exactly I am doing wrong.
Can any one help me to correct my mistake or point me about my issue.
Try this one
<?php
$args1 = array( 'numberposts' => 12 ,'orderby' => 'rand');
global $post;
//save the current post
$temp=$post;
$rand_posts1 = get_posts( $args1);
foreach( $rand_posts1 as $post ) ://yes this is required, we need $post in setup_postdata
setup_postdata($post); ?>
<?php the_title(); ?>
<?php endforeach;
$post=$temp;//restore current page
?>
That will do it.
Also please take a look at get_posts