I have a custom shortcode to be used on archive pages and list the first 3 posts. But how do I limit the posts displayed to the current category without hardcoding categories?
This is working, but shows posts from all categories.
function archive_loop_shortcode() {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
);
$my_query = null;
$my_query = new WP_query($args);
if($my_query->have_posts()):
while($my_query->have_posts()) : $my_query->the_post();
$custom = get_post_custom( get_the_ID() );
echo "<div>".get_the_post_thumbnail()."</div>";
echo "<h3>".get_the_title()."</h3>";
endwhile;
wp_reset_postdata();
else :
_e( 'Sorry, no posts matched your criteria.' );
endif;
}
add_shortcode( 'archive_loop', 'archive_loop_shortcode' );
Hope this code helps,
function archive_loop_shortcode() {
$current_cat = get_queried_object();
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
'tax_query' => array(
array(
'taxonomy' => $current_cat->taxonomy,
'field' => 'term_id',
'terms' => $current_cat->term_id,
),
),
);
$my_query = null;
$my_query = new WP_query( $args );
if ( $my_query->have_posts() ):
while ( $my_query->have_posts() ) : $my_query->the_post();
$custom = get_post_custom( get_the_ID() );
echo "<div>" . get_the_post_thumbnail() . "</div>";
echo "<h3>" . get_the_title() . "</h3>";
endwhile;
wp_reset_postdata();
else :
_e( 'Sorry, no posts matched your criteria.' );
endif;
}
add_shortcode( 'archive_loop', 'archive_loop_shortcode' );
Related
I tried a lot, but with no success. This LOOP works fine. It shows all Custom Post Types (apps-und-tools):
<?php
$loop = new WP_Query(
array(
'post_type' => 'apps-und-tools',
'posts_per_page' => -1,
)
);
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div>
<h2><?php the_title(); ?></h2>
</div>
<?php endwhile;
wp_reset_postdata();
Now I want to filter by a custom taxonomy. First I get the custom taxonomy with get_queried_object() and put the tax_query to the array in the LOOP. At the end are some control outputs.
<?php
// getting custom taxonomy of actual post
$this_term = get_queried_object();
$term_id = $this_term->term_id;
$term_name = $this_term->name;
$term_taxonomy = $this_term->taxonomy;
$term_slug = $this_term->slug;
// Wordpress Loop
$loop = new WP_Query(
array(
'post_type' => 'apps-und-tools',
'posts_per_page' => -1,
'tax_query' => array(
array (
'taxonomy' => $term_taxonomy,
'name' => $term_name
)
)
)
);
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div>
<h2><?php the_title(); ?></h2>
</div>
<?php endwhile;
wp_reset_postdata();
//Control Output
echo 'term_id: ' . $this_term->term_id;
echo '<br>';
echo 'name: ' . $this_term->name;
echo '<br>';
echo 'taxonomy: ' . $this_term->taxonomy;
echo '<br>';
echo 'slug: ' . $this_term->slug;
?>
The result is, that the LOOP does not show any results. No CPTs are shown, but the control output is as expected. I'd just like to filter my CPTs by custom taxonomy.
Any help is greatly appreciated, many thanks in advance.
Please check this loop to retrieve CPTs.
$args = array(
'post_type' => arra('apps-und-tools'),
'tax_query' => array(
array(
'taxonomy' => $term_taxonomy,
'field' => 'slug',
'terms' => $term_slug,
),
),
);
$query = new WP_Query( $args );
$term_taxonomy = 'taxonomy_name';
$args = array(
'post_type' => arra('apps-und-tools'),
'post_status' => "publish",
'tax_query' => array(
array(
'taxonomy' => $term_taxonomy,
'field' => 'slug',
'terms' => $search_key,
),
),
);
$query = new WP_Query( $args );
I have a post type called "rationale" and my taxonomy name is "company_list". In taxonomy there are list of company. Each company have many rationale.
I want to get latest rationale for each company. How can i do this ?
I try below code but it show all company list but data is duplicate
<?php
//$taxonomy = 'our_work_thematic';
$myquery = array (
'post_type' => 'rationale',
'paged'=>$paged,
'posts_per_page' => -1,
);
$loop = new WP_Query($myquery);
if( $loop->have_posts() ):
while( $loop->have_posts() ):
$loop->the_post(); global $post; ?>
<?php $terms = get_the_terms( $post->ID, 'company_list' );
foreach($terms as $term) {
$termlinks = get_term_link($term);
echo '<p class="post-content--cat">';
echo '' . $term->name . '';
echo '</p>';
}?>
<?php endwhile; ?>
<?php endif; ?>
You need to get latest term and use tax_query
$args_query = array(
'post_type' => array('rationale'),
'paged' => $paged,
'posts_per_page' => -1,
);
$terms = get_terms(array(
'taxonomy' => 'post_tag',
'hide_empty' => false,
));
if (!empty($terms) && is_array($terms)) {
$args_query['tax_query'] => array(
array(
'taxonomy' => 'company_list',
'field' => 'term_id',
'terms' => array($terms['0']->term_id), // single or array with id's
),
),
}
$query = new WP_Query($args_query);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$terms = get_the_terms($post->ID, 'company_list');
foreach ($terms as $term) {
$termlinks = get_term_link($term);
echo '<p class="post-content--cat">';
echo '' . $term->name . '';
echo '</p>';
}
}
} else {
// no post found
}
wp_reset_postdata();
try this
$terms = get_terms( array(
'taxonomy' => 'your taxonomy name',
'hide_empty' => false,
'orderby' => 'term_id',
'order' => 'asc',
) );
foreach ($terms as $terms_row) {
$terms_row->slug;
echo "<pre>";
print_r($terms_row);
echo "</pre>";
}
Thanks
i need to print all my post filtered by my current taxonomy's page.
which is the query with which I can have all these posts filtered for the current page taxonomy?
i'm trying to do a widget dynamic to use into more pages with different categories.
i hope someone can help me . thx :)
i'm trying with this code but doesn't work...
<?php
$args=array(
'post_type' => 'post,
'post_status' => 'publish',
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
solved:
<?php
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$the_query = new WP_Query( array(
'post_type' => 'post',
'tax_query' => array(
array (
'taxonomy' => $taxonomyName,
'field' => 'slug',
'terms' => $term_slug,
)
),
));
?>
I would like to display categories (from taxonomy-product_category) in archive-product.php.
Get categories:
Fruit
Herbs
Salad
Vegetables
See screenshot what I mean:
When I added coding in archive-product.php:
<?php
$args = array(
'post_type' => 'product',
'taxonomy' => 'product_category',
'hierarchical' => 1,
'nopaging' => false,
'posts_per_page' => '2',
'posts_per_archive_page' => '10',
'ignore_sticky_posts' => true,
'order' => 'rand',
);
echo '<div class="container">';
echo '<div class="row">';
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<div class="col-lg-4">';
echo '<a href="'.get_the_permalink().'">';
if ( has_post_thumbnail() ) {
the_post_thumbnail(array(486,226));
}
the_title();
the_content();
echo '</a>';
echo '</div>';
}
} else {
// no posts found
echo wpautop( 'Sorry, no posts were found' );
}
echo '</div>';
echo '</div>';
// Previous/next post navigation.
previous_post_link( '%link', 'Prev post in category', true
);
next_post_link( '%link', 'Next post in category', true );
// Restore original Post Data
wp_reset_postdata();
?>
But not display categories (Fruit, Herbs, Salad, Vegetables)
Would anyone know about this?
Thanks,
Shaun.
Please try below code:
$args = array(
'taxonomy'=> 'product_category',
'order' => 'DESC',
);
$categories = get_categories($args);
print_r($categories);
You can get the list of all product categories using Below code
$orderby = 'name';
$order = 'asc';
$hide_empty = false ;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
);
$product_categories = get_terms( 'product_cat', $cat_args );
if( !empty($product_categories) ){
echo '
<ul>';
foreach ($product_categories as $key => $category) {
echo '
<li>';
echo '<a href="'.get_term_link($category).'" >';
echo $category->name;
echo '</a>';
echo '</li>';
}
echo '</ul>
';
}
how can i get list of latest posts with their relative posts by tags?
example:
Latest post post title 1
related post
related post
Latest post post title 2
related post
related post
use this in Index.php
<?php
$args = array(
'numberposts' => 100,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' =>,
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true );
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
<ul>
<?php
foreach( $recent_posts as $recent )
{
echo '<li>'.$recent["post_title"];
$tags = wp_get_post_tags($recent["ID"]);
if ($tags)
{
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($recent["ID"]),
'posts_per_page'=>100,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() )
{
echo '<ul>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php
endwhile;
echo '</ul>';
}
wp_reset_query();
}
echo '</li>';
}
?>
</ul>
you can do something like this
your main loop contain recent post so during each loop use following function to get it's tag
$tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
then you can use another loop of using those tags
$query = new WP_Query( 'tag_id='.$tag_ids );
now $query has content you want.