Error trying to get the taxonomy term name - wordpress

Hello—so what I'm attempting is to get 2 posts from 2 taxonomy terms, I've got that part done, where I'm borking up is trying to display the term name above the posts. I've done this before using a foreach loop but no matter what I change, I keep getting the error that I've supplied invalid arguments for the loop. After a lot of googling, I'm a bit lost and wondering if you folks have some guidance for me?
$current_post_id = get_posts( array(
'post_type' => 'issue',
'posts_per_page' => 1,
'fields' => 'ids',
) );
$terms = get_the_terms( $current_post_id, 'department' );
foreach ( $terms as $term ) {
$args = array(
'connected_type' => 'posts_to_issues',
'connected_direction' => 'to',
'connected_items' => $current_post_id,
'post_type' => 'post',
'posts_per_page' => 2,
'tax_query' => array(
array(
'taxonomy' => 'department',
'field' => 'slug',
'terms' => array( 'washington-watch', 'equipment-spotlight'),
)
),
);
$sidebar_query = new WP_Query( $args );
while ( $sidebar_query->have_posts() ) : $sidebar_query->the_post(); ?>
<?php echo '<h2>' . $term->name . '</h2>' ?>
<figure>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'sidebar-thumb-med' ); ?>
</a>
</figure>
<h4><?php the_title(); ?></h4>
<?php the_excerpt();
endwhile;
}
// Restore global post data
wp_reset_query();

$current_post_id is an array with one item (because passing in a return fields argument returns an array). You need to refer to that one item in the following cases:
$terms = get_the_terms( $current_post_id[0], 'department' );
...and...
'connected_items' => $current_post_id[0],

Related

Custom Post Type filtering by own taxonomies

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 );

Wordpress: I want latest one product from all categories display on the page in wordpress?

I want latest one product from all categories display on the page in wordpress , when we add more categories and products in it then it should have to be add(display) on the page with its single latest product.How we can do this? please help me. Thank you
First, you need to get all the product categories with at least a single post using the hide_empty.
Then loop through each category and run a query for each to get the single product.
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true
);
$product_categories = get_terms( 'product_cat', $args );
$count = count($product_categories);
if ( $count > 0 ){
foreach ( $product_categories as $product_category ) {
echo '<h4>' . $product_category->name . '</h4>';
$args = array(
'posts_per_page' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $product_category->slug
)
),
);
$products = new WP_Query( $args );
echo "<ul>";
while ( $products->have_posts() ) {
$products->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php
}
wp_reset_postdata();
echo "</ul>";
}
}

need to print post filtered by my current taxonomy's page?

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,
)
),
));
?>

How to list all category from custom post type?

I have a post type called 'dining' and has a taxonomy called 'dining-category'.
What I want to do is, I want to display all the category from post type 'dining' in my footer area.
In WordPress 4.6 get_terms is deprecated. So there is an alternate of this (get_categories) Read this
And here is Example code:
<?php
$args = array(
'taxonomy' => 'dining-category',
'orderby' => 'name',
'order' => 'ASC'
);
$cats = get_categories($args);
foreach($cats as $cat) {
?>
<a href="<?php echo get_category_link( $cat->term_id ) ?>">
<?php echo $cat->name; ?>
</a>
<?php
}
?>
Hope this will help you.
<?php
$args = array(
'type' => 'dining',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'taxonomy' => 'dining-category',
'pad_counts' => false );
$categories = get_categories($args);
echo '<ul>';
foreach ($categories as $category) {
$url = get_term_link($category);?>
<li><?php echo $category->name; ?></li>
<?php
}
echo '</ul>';
?>
If category not assigned any post it will not show. therefore assign any post. This code running perfectly.
<?php
$wcatTerms = get_terms(
'category', array('hide_empty' => 0, 'number' => 3, 'order' =>'asc', 'parent' =>0));
foreach($wcatTerms as $wcatTerm) :
?>
<small><?php echo $wcatTerm->name; ?></small>
<?php
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $wcatTerm->slug,
)
),
'posts_per_page' => 1
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$imgurl = get_the_post_thumbnail_url( get_the_ID(), 'full' );
$title=get_the_title($post->ID);
?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php endwhile; wp_reset_postdata(); ?>
<?php endforeach; ?>
use get_terms() function to fetch custom taxonomy by its slug, in your case slug is dining-category.
read function refrence from wordpress codex website and try this.

how to get related posts list in home page

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.

Resources