SOLVED: MB Relationship query improvement in nested Loop

In Metabox, I have made a relationship between a custom post type and a product.
In the single product template, I made a simple relationship query which lists the appropriate associated custom post titles in the builder

When I goto bricks preview mode, it shows up fine.

When I then click “view on frontend”, the query loop returns nothing

I thought to myself, maybe because this is a preview of a template and bricks is confused on specificity. So, I applied this template to the same product that I had used to populate content and then looked at the product itself on the font end

Same result - query loop returns nothing on the front end.

As a test, I then created a very simple text query loop to see what the issue is


image

And that list of post titles shows up just fine on the front end.

Not sure what’s going on.

I do have the relationship query loop inside a outer query loop (applied on the section), which is looping through a custom field group attached to the product. However, the queries are unrelated.

It works on the backend…why not the front end?

Hi Eric,
Thanks so much for your report!

Unfortunately, I cannot reproduce the issue. Have you already tested it with Bricks 1.9.5? If it doesn’t work either:

Would you be so kind as to send temporary login credentials, information on where we have to look, and a link to this thread to help@bricksbuilder.io using the email address you used during the purchase?

Best regards,
timmse

I also have problems mit MB Relationships not showing in Bricks 1.95.
In the backend it shows a querry result, but its standard posts, not from my connectec CPT. In the frontend there is nothing.

@lynxm is it showing the correct posts in the backend?

Interesting, no. I’ve never seen that problem before.

I’m sure you’ve already done this, but i would check your MB relationship set-up first. Kevin Geary actually has a good video walking through how to set it up properly (i.e., not reciprocal relationship, etc) with bricks

1 Like

Thanks, I did watch Kevins Tutorial again and everything was set up correctly.
I opened a metabox support forum thread and hope to get help there: Connecting an Event (TEC Pro) with a CPT | Bricks Builder - Meta Box

That said: All the other MB Relationships on my page do work, its just not working with events (The Events Calendar Pro).

Hi @lynxm ,

We discussed your issue internally.
As per my explanation, your setup will not work as the Relationship loop located inside a Group loop (nested). Hence, the context used is not correct.

However, we recorded this as an improvement task.
What we will do is, if the Relationship loop sit inside a nested loop, and the loop context is not a Post, User or Term, we will use the current page/post as the context. So it should do the job for your scenario.

Regards,
Jenn.

2 Likes
<?php
$restaurante_id = get_the_ID();

// Obtener términos de la taxonomía 'tipo-de-plato' asociados al restaurante actual
$terms = get_terms(array(
    'taxonomy' => 'tipo-de-plato',
    'hide_empty' => false,
    'meta_key' => 'orden-tipo-plato',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
));

if (!empty($terms) && !is_wp_error($terms)) {
    foreach ($terms as $term) {
        // Consultar los platos asociados a este término
        $args = array(
            'post_type' => 'platos-carta',
            'posts_per_page' => -1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'tipo-de-plato',
                    'field' => 'term_id',
                    'terms' => $term->term_id,
                ),
            ),
            'meta_query' => array(
                'relation' => 'AND',
                array(
                    'key' => 'carta_restaurante_id',
                    'value' => '"' . $restaurante_id . '"',
                    'compare' => 'LIKE'
                ),
                array(
                    'key' => 'orden',
                    'type' => 'NUMERIC',
                    'compare' => 'EXISTS',
                ),
            ),
            'orderby' => 'meta_value_num',
            'meta_key' => 'orden',
            'order' => 'ASC'
        );

        // Ejecutar la consulta
        $platos = get_posts($args);

        // Verificar si hay platos asociados a este término
        if (!empty($platos)) {
            // Imprimir el nombre del término
            echo '<section id="'.$term->slug.'"class="tipo-de-plato caja-cat-plato-video" data-tipo-plato="'.$term->slug.'">';
            echo '<h2 id="'.$term->name.'">' . $term->slug . '</h2>';
            
            // Iterar sobre los platos
         foreach ($platos as $plato) {
                // Obtener el precio del plato
                $precio = get_post_meta($plato->ID, 'precio', true);
              
                 $video_link = get_post_meta($plato->ID, 'link-video', true);

                // Mostrar informaciĂłn de cada plato
                ?>

			<div class="plato">
                <video class="video-fondo" autoplay loop playsinline muted>
						<source src="<?php echo $video_link ?>">
						Tu navegador no admite el elemento de video.
				</video>
                  <div class="contenedor-contenido-plato">  
					<div class="contenido-plato">
						<h3><?php echo $plato->post_title; ?></h3>
						<?php if (!empty($precio)) { ?>
							<div class="precio"><?php echo $precio; ?></div>
					<?php } ?>
					</div>
					<div class="alergenos">
                   
					<?php
						$alergenos = wp_get_post_terms($plato->ID,'alergenos');
						foreach ($alergenos as $alergeno) {
							$id_icono = get_term_meta($alergeno->term_id, 'icono', true);
							$term_name = $alergeno->name;
							echo '<img width="32" src="' . wp_get_attachment_url($id_icono) . '" title="' . $term_name . '">';
						}
              
					?>
                    
					</div>
					
				</div>                   
            
				
                  
				
			</div>

            <?php
         }
          

            echo '</section>'; // Cerrar tipo-de-plato
        }
    }
} else {
    // Si no hay términos de taxonomía asociados al restaurante actual
    echo 'No se encontraron términos de taxonomía asociados a este restaurante.';
}
?>

Sorry for the spanish comments in the code but chatGPT helped me on that.
My scenario is similar:
Relation: Menu ->Dishes
Dishes have “dishes types” terms
I need to show the dishes grouped by their terms for every Menu CTP.
I’m currently using that custom code and it works, but I would like to use the query loop

Thank you in advance

I think I have the same problem. I have a query on a Metabox group and inside that i have another query for a Metabox relationship. It will just not work not matter what i do. Is this what you were describing is not possible? Has the improvement been made?

Hi @Jules ,

Unfortunately, this task is still in progress.
We will update the thread if it’s included in a new release.

Regards,
Jenn

Hi guys,
We’ve fixed this issue in Bricks 1.11.1, now available as a one-click update in your WordPress Dashboard.

Changelog: Bricks 1.11.1 Changelog – Bricks

Please let us know if you are still experiencing issues.

Best regards,
timmse

1 Like