Wordpress: How to add the OR operator to this query? - wordpress

I have this piece of code to count the number of posts in a custom taxonomy term and its child terms:
function wp_get_postcount($id)
{
$count = 0;
$taxonomy = 'productcategories';
$args = array(
'child_of' => $id
);
$tax_terms = get_terms($taxonomy,$args);
var_dump($tax_terms);
foreach ($tax_terms as $tax_term) {
$count +=$tax_term->count;
}
return $count;
}
The problem is, it returns as a null for the actual term that contains the post, because that has no child terms. I'd like to know if its possible for the query to include the term with the $id and its children as well?
Thank you!

Why not use 'pad_counts' in get_terms.
$terms=get_terms('my_taxonomy',array('pad_counts'=>1));

Related

Rendering only published taxonomy

It's rendering taxonmy terms. I need only published terms. There are two languages in site. Publish status can be translated. I need help with condition. I don't work with php. Could anyone help?
foreach ($child_terms as $term) {
$taxonomy_term = \Drupal\taxonomy\Entity\Term::load($term->tid);
$taxonomy_term_trans = \Drupal::service('entity.repository')->getTranslationFromContext($taxonomy_term, $curr_langcode);
if (????) {
$child_term_options[$term->tid] = $taxonomy_term_trans->getName();
}
}
Assuming you pretend to loop terms that are children of some given term.
Assuming you know the term id of the parent term.
<?php
$status = TRUE;
$lang_code = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
$child_terms = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadByProperties([
'parent' => $parent_tid,
'status' => $status,
'langcode' => $lang_code,
]);
foreach ($child_terms as $term) {
$name = $term->getName();
$tid = $term->id();
}

List all posts without at least one category assigned to them in admin

I am trying to allow the admin user to list out CPT that have not already had a category assigned to them (This is to enable them to quickly find posts that require a category and edit them)
I have this so far -
function function_name( $query ) {
global $post_type;
if ( is_admin() && $post_type == 'product' ) {
$query->set( 'cat', '' );
}
}
add_action( 'pre_get_posts', 'function_name' );
This does not work however. Can someone help point me in the right direction?
Thanks
I have achieved what I need this way, if anyone else comes up with this question -
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
) );
$arr = array();
foreach ($terms as $term) {
$arr[] = '-'.$term->term_id;
}
$query->set( 'cat', $arr );
I am simply getting all category id's. looping through these and appending them to another array with a munis "-" value and passing them through to the query as an argument.

Get no of posts inside with a term for a custom taxonomy

How can i get the no of posts inside a term of a custom taxonomy ? (including the posts that are attached to a child term).
For example i have:
term (2 posts)
-child term (2 posts)
--child child term (1 post)
Right now i'm, doing it like this:
$categories = get_terms($taxonomy,$args);
foreach ($categories as $categ) {
print $categ->name.' / '.$categ->count;
}
But for "term" i get only 2 posts when i really need to show 5( 2 from "term" and 3 from it's children).
Thanks
There is an easier way to do this: do a standard WP_Query with taxonomy parameters:
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'your_custom_taxonomy',
'field' => 'slug',
'terms' => 'your-parent-term-name',
),
),
);
$query = new WP_Query( $args );
// Get the count
$count = $query->post_count;
If you don't know the name(s) of terms within a taxonomy, you can do a query and pass their IDs as an array to WP_Query. See this post on WPSE for more info.
This is something I run into few times, and I changed the code from PatchRanger on bountify to make it work with taxonomies (so please note this solution is based on his work):
function wp_get_term_postcount($term) {
$count = (int) $term->count;
$tax_terms = get_terms($term->taxonomy, array('child_of' => $term->term_id));
foreach ($tax_terms as $tax_term) {
$count += wp_get_term_postcount_recursive($tax_term->term_id);
}
return $count;
}
function wp_get_term_postcount_recursive($term_id, $excludes = array()) {
$count = 0;
foreach ($tax_terms as $tax_term) {
$tax_term_terms = get_terms($tax_term->name, array(
'child_of' => $tax_term->term_id,
'exclude' => $excludes,
));
$count += $tax_term->count;
$excludes[] = $tax_term->term_id;
$count += wp_get_term_postcount_recursive($tax_term->term_id, $excludes);
}
return $count;
}
The recursive function is there to prevent double counting of childs. You can add those two functions inside functions.php.
Then update your code to use it:
$categories = get_terms($taxonomy, $args);
foreach($categories as $categ) {
print $categ->name.' / 'wp_get_term_postcount($categ);
}

Wordpress function : get_posts

Crazy night! ;P
I'm devoliping a plugin with Wordpress and I'm lost looking for a function.
What I did is,
$args = array("meta_key" => "email", "meta_value" =>$email);
$posts = get_posts($args);
I did that in order to filter custom fields with the same email,
if (count($posts) < 0){ // do something }
The problem is that get_posts didn't work with custom posts,
$args = array('post_type' => 'reserva', "meta_key" => "wpcf-email", "meta_value" =>$email);
I like to find something like get_posts in order to count it.
I think I have a bad solution,: to loop it, but I want to array it.
Any idea?
Thanks,
Best regards,
Use meta_query in your arguments array.
Try this, if you want post related to meta_key and meta_value so please use this
http://codex.wordpress.org/Class_Reference/WP_Meta_Query
thanks a lot, I will check it now,
As I said tonight i did an ugly solution,
I made a loop with a counter in porder to find if a custom fields exist.
Here is the ugly code,
<?php // Find matches in csutom post types
$my_consulta = array(
'post_type' => 'reserva',
'post_status' => 'draft',
'post_key' => 'wpcf-email',
'meta_value' => $email,
);
$wp_query = new WP_Query($my_consulta);
$count = 0;
while ( have_posts() ) : the_post();
$count = $count + 1;
endwhile;
if ($count == 0){
//No matches
} else {
//There is one or more matches
}
?>
Is a good way?
Best regards,
Cristian

WooCommerce - show product child category from specific parent with permalink & html

With regards to this question which was nicely solved:
WooCommerce - show product child category from specific parent
I managed to get it working with this code:
dd_action( 'woocommerce_single_product_summary', 'wpse124955_test', 99 );
function wpse124955_test() {
$taxonomy = 'product_cat';
$term_id = wp_get_post_terms(get_the_ID(), $taxonomy, array("fields" => "ids"));
$parent = '21';
$args = array(
'fields' => 'ids',
'child_of' => $parent
);
$branch_ids = get_terms( $taxonomy, $args );
$intersect_ids = array_intersect($term_id, $branch_ids);
foreach ( $intersect_ids as $tid ) {
$tobj = get_term_by('id', $tid, 'product_cat');
$name_arr[] = $tobj->name;
}
$term_list = implode(', ', $name_arr);
echo $term_list;
}
The child category gets printed nicely with this code.
Now I would like to have the child category linked (to itself) and also enclosed with a
<h1></h1>
so that i can style it with my css, but being a complete PHP idiot i have no idea how to edit the code above.
Have trawled through the internet for hours without a straightforward answer. This was the best solution - just needs some tweaking! If anyone would help i would be eternally grateful! Thank you!
$name_arr[] = $tobj->name;
to
$name_arr[] = '<h1>'.$tobj->name.'</h1>';
should work.

Resources