limit posts pulled in from get_terms - wordpress

I have the following code, which basically pulls in a list of terms for the taxonomy "categories". It then pulls in every post for that term.
$terms = get_terms('categories');
foreach ($terms as $term) {
$wpq = array ('taxonomy'=>'categories','term'=>$term->slug);
$myquery = new WP_Query ($wpq);
$article_count = $myquery->post_count;
echo "<h3 class=\"term-heading\" id=\"".$term->slug."\">";
echo $term->name;
echo "</h3>";
if ($article_count) {
echo "<ul>";
while ($myquery->have_posts()) : $myquery->the_post();
echo "<li>".$post->post_title."</li>";
endwhile;
echo "</ul>";
}
}
My question is how would i go about limiting the query to only pull in 1 post from each term?
Any help would be greatly appreciated, Cheers Dan

You can use post_count in $wpq array
eg:- $wpq = array ('taxonomy'=>'categories','term'=>$term->slug,'post_count' => 1);
More about WP_Query http://codex.wordpress.org/Class_Reference/WP_Query

Here is what i got working:
i simply changed the following:
$wpq = array ('taxonomy'=>'categories','term'=>$term->slug,);
to:
$wpq = array ('taxonomy'=>'categories', 'showposts' => 1, 'term'=>$term->slug,);

Related

How an i get product category name from it's id in woocommerce?

I want to create a shortcode for my wp+woocommerce site, that will show the name of the current products category. I get the category id from my url with get request - this is a specialty. It will look something like:
function this_product_category_name() {
$cat_id = (int) str_replace('-product_cat', '', $_GET['really_curr_tax']);
// here must be a string to get the $cat_name from $cat_id;
echo $cat_name;
}
add_shortcode( 'this_product_category_name', 'this_product_category_name' );
What can be the solution?
Use get_term_by().
function this_product_category_name() {
$cat_id = (int) str_replace('-product_cat', '', $_GET['really_curr_tax']);
$product_cat = get_term_by( 'id', $cat_id, 'product_cat' );
echo $product_cat->name;
}
add_shortcode( 'this_product_category_name', 'this_product_category_name' );
USEFUL LINKS
get_term_by()
Since you're on the product category page, then you could use get_queried_object, and then from that object you could get the name like so:
$cat = get_queried_object();
echo $cat->name;
//or if you want to get its id
echo $cat->term_id;
//if you want to see the object and what's in it then you could use print_r()
print_r($cat);
Let me know if that's what you're looking for.
So your code would be something like this:
function this_product_category_name()
{
$cat = get_queried_object();
echo $cat->name;
}
add_shortcode( 'this_product_category_name', 'this_product_category_name' );

Display custom Field in loop - Woocommerce category

I have nested categories in my website.
I created a custom Field in Woocommerce category and I tried to Add it in category loop. It shows only the term value of the current category page
add_action( 'woocommerce_after_subcategory_title', 'custom_add_cat_Min_price', 12);
function custom_add_cat_Min_price ($category) {
$prix_min_catt = get_term_meta(get_queried_object_id(), 'prix_min_cat', true);
$terms = get_the_terms( $post->ID, 'prix_min_cat' );
foreach ($terms as $term){
echo '<div class="prixminofcatg">'.$prix_min_catt.'</div>';
}
}
I think the problem is the scope of your function. You've passed the $category to your function, but haven't used it. This gives you the ID of your category:
function custom_add_cat_Min_price ($category) {
$category_id = $category->term_id;
and from there, you should be able to extract the custom fields.
Thanks its working without Foreach
add_action( 'woocommerce_after_subcategory_title', 'custom_addd_cat_Min_price', 29);
function custom_addd_cat_Min_price ($category) {
$category_id = $category->term_id;
$prix_min_cag = get_term_meta($category_id, 'prix_min_cat', true);
$terms = get_term( $category_id, 'prix_min_cat' );
echo '<div class="prixminofcatg">'.$prix_min_cag.'</div>';
}

How to list all users of posted in wordpress

$args = array(
'orderby' => 'display_name'
);
$wp_user_query = new WP_User_Query($args);
$authors = $wp_user_query->get_results();
if (!empty($authors))
{
echo '<ul>';
foreach ($authors as $author)
{
$author_info = get_userdata($author->ID);
echo '<li>'.$author->ID.' '.$author_info->first_name.' '.$author_info->last_name.'</li>';
}
echo '</ul>';
} else {
echo 'No authors found';
}
I'm using post filter by post author. Above code displays all the users so need to display only authors who posted in the blog.
just like wp_list_authors() function but i need to get the author id intead of author name. because I need to create a dropdown list. While someone change the option, then I need to get the post by that author in AJAX
Update 1:
Hope this help:
$posted = get_posts([
'post_type' => 'your_custom_post_type',
]);
$author_ids = [];
foreach ($posted as $post) {
$author_ids[] = $post->post_author;
}
$author_ids = array_unique($author_ids);
if (!empty($author_ids) )
{
echo '<ul>';
foreach ($author_ids as $user_id)
{
$author_info = get_userdata($user_id);
echo '<li>'.$user_id.' '.$author_info->first_name.' '.$author_info->last_name.'</li>';
}
echo '</ul>';
} else {
echo 'No authors found';
}
Make sure to change post_type to your post type and each user has both first_name and last_name.
There's a missing argument on Codex reference: has_published_posts.
$args = [
'orderby' => 'display_name',
'has_published_posts' => true
];
$authors = new WP_User_Query($args);
...
It's better to look for inside class file because info on Codex is not always up-to-date.

how to list a repeater sub fields with label and value in Advanced Custom Field?

I've searched and I could not find any solution to list a repeater field rows with Label of sub field and its value.
in my case I want to list a repeater field sub fields with Label and value.
for example :
'Sub Field Label' = 'Value'
is there any way to do this ?
If you know the labels you want to retrieve from your Repeater Field, just use the standard method:
if( have_rows('repeater_field_name') ):
while ( have_rows('repeater_field_name') ) : the_row();
echo 'Label = ' . get_sub_field('sub_field_name') . '<br>';
endwhile;
endif;
If you aren't in a single post/page or outside The Loop, just add the $post_id as the second parameter to your ACF function calls. For example: have_rows('repeater_field_name', $post_id).
If you don't know the label names, I guess you could use get_fields() to get an array of all custom fields for the current post and iterate it. Something like:
$fields = get_fields($post->ID);
foreach ($fields as $field_type => $field) {
if ( $field_type == 'repeater_field' ) {
foreach ($field as $row) {
foreach ($row as $label => $value) {
// In this case you should be aware that
// $value could be an Array too...
echo $label . ' = ' . $value;
}
}
}
}
Anyway, I recommend you to take a look at ACF Documentation. It's complete, clear and with lots of code snippets covering the most common uses.
<?php $args = array('post_type' => 'post');
$the_query = new WP_Query( $args );
query_posts( $args );
while ( have_posts() ) : the_post();
$field = get_field_object('field_name');
echo $field['label']; //print label name
echo the_field('field_name'); //and its value
endwhile; wp_reset_query(); ?>
please try this hope help to you

Wordpress - Display Taxonomy Terms without Links - (get_terms, the_terms, wp_tag_cloud)

Is there a way to display Taxonomy Terms without them being a link?
I basically want to display all the terms underneath my Taxonomy of "Headings," but without them being links.
I've tried multiple solutions such as get_terms, get_the_terms, the_terms, wp_tag_cloud but I was unable to find a solution.
Thanks.
The solution of bizarre returned all terms, if you need to display only the terms per post item you can use this:
$terms = wp_get_post_terms( $post->ID, 'taxonomy-term', array("fields" => "names") );
echo implode(', ',$terms);
Make sure to get the right "taxonomy-term" correct!
below code retrieve all terms name of a taxonomy :
$terms = get_terms( 'cars', array('fields' => 'names'));
$terms = get_terms("taxonomy");
$count = count($terms);
if($count > 0)
{
echo '<ul>';
foreach ($terms as $term)
{
echo '<li>'.$term->name.'</li>';
}
echo '</ul>';
}

Resources