Wordpress, foreach on taxonomy shows duplicates - php

I have the below code, it works initially with regards to the fact that it displays the products for all taxonomies. However if a product is set to 2 taxonomies then it will display twice on the page as opposed to showing the first instance of the product.
<?php if ( $terms && !is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$args = array(
'post_type' => 'products',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $term->slug,
),
),
'order' => 'asc',
);
runQuery($args);
}
} ?>
Here is the runQuery function:
<?php $x = 0;
function runQuery($args) {
global $x;
$query = new WP_Query( $args );
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
$cat_terms = get_the_terms($post->id, 'product_cat');
$datagroups = '';
foreach ($cat_terms as $key => $cat) {
if (count($cat_terms) == ($key + 1)) {
$datagroups .= '"' . $cat->name . '"';
} else {
$datagroups .= '"' . $cat->name . '", ';
}
}
?>
HTML Here that is displayed;
<?php $x ++;
endwhile;
endif;
wp_reset_postdata();
}?>

I have figured this out, I essentially just had to check the current post in the loop to see if it had already been displayed:
<?php $x = 0;
$displayed = [];
function runQuery($args) {
global $displayed;
global $x;
$query = new WP_Query( $args );
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
$cat_terms = get_the_terms($post->id, 'product_cat');
$datagroups = '';
if ( in_array( get_the_ID(), $displayed ) ){
continue;
}
// update array with currently displayed post ID
$displayed[] = get_the_ID();
foreach ($cat_terms as $key => $cat) {
if (count($cat_terms) == ($key + 1)) {
$datagroups .= '"' . $cat->name . '"';
} else {
$datagroups .= '"' . $cat->name . '", ';
}
}
?>
Source that may help others: https://wordpress.stackexchange.com/questions/285091/avoid-duplicate-post-from-same-taxonomy

Related

ACF Taxonomy only returning child - want both parent and child

I have the following function:
function my_acf_load_field( $field )
{
global $post;
global $current_user;
$field['choices'] = array();
wp_reset_query();
$query = new WP_Query(array(
'post_type' => 'gear',
'orderby' => 'title',
'order' => 'ASC',
'author' => $current_user->ID,
'posts_per_page' => -1,
));
foreach($query->posts as $product_id=>$macthed_product){
$zz = get_field('brand_preselect',$macthed_product->ID);
$yy = get_field('category_tax',$macthed_product->ID);
$aa = get_field('weight',$macthed_product->ID);
foreach( $yy as $term ):
$choices[$macthed_product->ID] = '<u class="cat">' . $term->name . '</u>' . '<i class="brand">' . $zz . '</i>' . $macthed_product->post_title . ' (' . $aa . ' kg)';
endforeach;
}
$field['choices'] = array();
if( is_array($choices) )
{
foreach( $choices as $key=>$choice )
{
$field['choices'][$key] = $choice;
}
}
// wp_reset_query();
return $field;
}
add_filter('acf/load_field/name=choices', 'my_acf_load_field');
This works well in that it populates the checkboxes with the category name, brand and title. For some reason $term->name in the foreach only shows the child category. If I echo out $term->name before the $choices[$macthed_product->ID] it shows all that apply. How do I get $term->name in the foreach to show all taxonomy matches (parent and child)

How to search for a string in a category name?

I use the search result and I GET
$s = $_GET['s'];
Which gives: "mario"
Now there is a category named Mario Bianchi under a main category called Persone
When I GET $s I need to get all posts within that category, I tried the following but I get nothing
$terms = get_terms( 'category', array(
'name__like' => $s,
'hide_empty' => true // Optional
) );
if ( count($terms) > 0 ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>' . esc_html( $term->name ) . '</li>';
}
echo '</ul>';
}
Yet I need the actual post attached, not the category itself
get_terms does just that, it get the terms for your query. What you'll want is WP_Query with a tax_query. You've already got the categories you want to return the posts for, so it shouldn't be too difficult
$terms = get_terms( array(
'taxonomy' => 'category',
'name__like' => $s,
'hide_empty' => true // Optional
) );
$term_ids = array();
if ( ! empty( $terms ) ) {
foreach( $terms as $term ) {
$term_ids[] = $term->term_id;
}
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => $term_ids,
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . esc_html( get_the_title() ) . '</li>';
}
echo '</ul>';
}
wp_reset_postdata();
}

WordPress custom post type array not working as expcted

I'm using this code...
function include_post_types() {
$post_types = get_post_types( array (
'show_ui' => true
),
'objects' );
$return = '';
foreach ( $post_types as $post_type ) {
$my_sitemap_options = get_option( 'my_sitemap_settings' );
$post_type_name = $post_type->name;
if ($my_sitemap_options['post_types'] && in_array($post_type_name, $my_sitemap_options['post_types'])) {
$the_excluded = $post_type_name;
$return .= "'" . $the_excluded . "', ";
}
}
return $return;
}
... to return a list of custom post types that I have selected from an options page. This works fine, and if I do this...
echo included_post_types();
...I see this...
'clothes', 'shoes', 'jackets',
...which is what I excpected.
But the problem is when I try to use included_post_types() in a loop to only show posts with those post types, like this:
$sitemap_post_args = array(
'post_type' => array(included_post_types()),
'posts_per_page' => -1,
'orderby' =>'post_type',
'order' =>'asc'
);
$loop = new WP_Query($sitemap_post_args);
global $post_type;
global $post;
echo '<ul>';
$last_post_type = '';
while($loop->have_posts()): $loop->the_post();
$current_post_type = $post->post_type;
$current_post_type_object = get_post_type_object( $current_post_type );
if($current_post_type != $last_post_type) echo "<br /><li><strong>" . $current_post_type_object->labels->name . "</strong></li>";?>
<li><?php echo get_the_title(); ?></li>
<?php echo "\n";
$last_post_type = $current_post_type;
endwhile;
wp_reset_query();
echo '</ul>';
It simply doesn't display anything on my page, but it doesn't throw an error either.
I'm almost certain the problem is this line:
'post_type' => array(included_post_types()),
I even tried it like this...
'post_type' => included_post_types(),
...but it did not work.
If I try this...
'post_type' => array('clothes', 'shoes', 'jackets', ),
...it works, but I need to be able to use the function name.
Any suggestions would be helpful.
please replace below code with yours. It should be help.
function include_post_types() {
$post_types = get_post_types( array (
'show_ui' => true
),
'objects' );
$return = array();
foreach ( $post_types as $post_type ) {
$my_sitemap_options = get_option( 'my_sitemap_settings' );
$post_type_name = $post_type->name;
if ($my_sitemap_options['post_types'] && in_array($post_type_name, $my_sitemap_options['post_types'])) {
$the_excluded = $post_type_name;
$return[] = $the_excluded;
}
}
return $return;
}
and also replace below code that show posts
$post_type_list = include_post_types();
$sitemap_post_args = array(
'post_type' => $post_type_list,
'posts_per_page' => -1,
'orderby' =>'post_type',
'order' =>'asc'
);

Extend taxonomy search form to include custom post type fields

I have the following to create a search form for taxonomies, from the "exam" custom post type.
What I'd like to do is extend the search to include a field with name "examination_code" from the "exam" custom post type.
function buildSelect($tax){
$terms = get_terms($tax);
$x = '<select multiple="multiple" name="'. $tax .'[]">';
$x .= '<option value="">Select '. ucfirst($tax) .'</option>';
foreach ($terms as $term) {
$x .= '<option value="' . $term->slug . '">' . $term->name . '</option>';
}
$x .= '</select>';
return $x;
}
The search form:
<form method="post" action="<?php bloginfo('url'); ?>/timetable-results/">
<?php
$taxonomies = get_object_taxonomies('exam');
$taxonomies = array_diff($taxonomies, ["language", "post_translations"]);
foreach($taxonomies as $tax){
echo buildSelect($tax);
}
?>
<input type="submit"/>
</form>
The results page:
<?php
$tax_query = array();
foreach ( get_object_taxonomies( 'exam' ) as $tax ) {
if ( isset( $_POST[ $tax ] ) ) {
$tax_query[] = array(
'taxonomy' => $tax,
'terms' => wp_unslash( ( array ) $_POST[ $tax ] ),
'field' => 'slug',
);
}
}
$args['tax_query'] = $tax_query;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args['paged'] = $paged;
$the_query = new WP_Query( $args );
var_dump($the_query);
?>

wordpress sub taxonomies are not showing properly

I want to display only 1st level of sub taxonomies, below is my code,it's just not working, can anyone please tell whats wrong with my code?? Many thanks.
<?php
$term_id = 31;
$taxonomy_name = 'kosher_category';
$args = array('child_of' => $term_id, 'parent' => $term_id);
$termchildren = get_terms($taxonomy_name, $args );
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
?>
Add hide_empty.
<?php
$term_id = 31;
$taxonomy_name = 'kosher_category';
$args = array(
'child_of' => $term_id,
'parent' => $term_id,
'hide_empty' => false // ADDED
);
$termchildren = get_terms($taxonomy_name, $args );
echo '<ul>';
foreach($termchildren AS $child) {
$term = get_term_by('id', $child, $taxonomy_name);
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
?>

Categories