Use GB query loop for Related Post

Hello !

It’s been a while Tom helped me to use WP Show Post to display Related Posts with a custom PHP function.

While waiting for the new GB PRO features to be added, if I want to stop using WPSP would there be a solution to adapt this PHP function and use it with GB query loop ?

Thanks !

Here is the PHP function I was using with WPSP :

<?php if ( is_single() ) {
$cat_values = array();

$cats = get_the_category();

foreach ( $cats as $cat ) {
$cat_values[] = $cat->slug;
}
} else {
$cat_values = get_category( get_query_var( 'cat' ) );
}

// Remove first category as long as there's more than 4 categories.
if ( count( $cat_values ) > 1 ) {
unset( $cat_values[0] );
}

$cat_slug = implode( ', ', $cat_values );
$list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
wpsp_display( $list->ID, 'tax_term="' . $cat_slug . '"' );
?>

Hi there,

In 1.5.0-beta.2, you’ll be able to do this:

  1. Add a Query Loop
  2. Select the Grid block under the Query Loop
  3. Give it a class name: my-class-name
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'my-class-name' ) !== false ) {
        if ( is_single() ) {
            $cat_values = array();
            $cats = get_the_category();

            foreach ( $cats as $cat ) {
                $cat_values[] = $cat->slug;
            }
        } else {
            $cat_values = get_category( get_query_var( 'cat' ) );
        }

        // Remove first category as long as there's more than 4 categories.
        if ( count( $cat_values ) > 1 ) {
            unset( $cat_values[0] );
        }

        $cat_slug = implode( ', ', $cat_values );
        $query_args['tax_query'] = array(
            array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => $cat_slug,
            ),
        );
    }

    return $query_args;
}, 10, 2 );

That should do the trick :slight_smile:

2 Likes

Regarding to display random posts, I reckon I need a modified code for that, right?

Thanks

Hi Tom

Nice, it’s just what I’ve been waiting for.

Is there an estimated date for 1.5.0-beta.2?
Tnx

beta.2 should be released on Tuesday :slight_smile:

2 Likes

Hi,

I’m trying this it already with beta 2 and I’m either doing something wrong or it doesn’t work.
Can anyone also try to confirm?

Thanks Tom, will give it a try when 1.5-beta.2 is released.

1.5.0-beta.2 has not been released, so it would explain why it’s not working for now.

It was released yesterday (Or we need beta 2 PRO version)

My bad, as it wasn’t in my accounts downloads I thought it wasn’t released.

I’ve tried it by the way and it does not work for me either.

1 Like

No problem :stuck_out_tongue: , I’ll keep an eye on the thread in case the solution appears. Regards.

I’ve updated the instructions above. Try adding the class name to the Grid block within the Query Loop block.

1 Like

Not working either for me with the update @Tom

Here is what I did :

  • Create a Hook Bloc Element
  • Hook name “after_conter”
  • Add a Queryloop and set “post type” to “posts” and “post per page” to “'4”
  • Add class name to the Grid block within the Query loop block
  • Display the hook only on posts.

Filter is added in my function.php

You can check it here at the end of the page you’ll see the actual “Related post” bloc used with WPSP and then the new block with GB query block. And it does not display related post in same category.

It doesn’t work for me either with exactly the same conditions of @Samlaflam

Staging to see.

Above return $query_args;, add this: var_dump($query_args);.

What’s the output shown on the frontend?

Hi @Tom
Critical error in frontend and this warning in the code:

Sorry, missing a semi-colon. Updated the code above.

Many thanks for your time @Tom

Ok, now there is no critical error in frontend but you can see this above posts (not related).

array(5) { [“post_type”]=> string(4) “post” [“posts_per_page”]=> int(4) [“paged”]=> int(1) [“offset”]=> int(0) [“tax_term”]=> string(7) “vinetas” }

WP Show Post is not involved (i think) because it still appears if I disable the WPSP

You can see it in any post on https://staging.jrmora.com

Is vinetas the correct term?

@Tom Same here,

Front-end I have this message above the block (that is not displaying related posts but latest posts instead) :

“array(5) { [“post_type”]=> string(4) “post” [“posts_per_page”]=> int(4) [“paged”]=> int(1) [“offset”]=> int(0) [“tax_term”]=> string(19) “culture-colombienne” }”

Back-end the block is not displaying with message “No result found”

When saving I have this message : “invalid JSON”

When refreshing this message appears briefly : “array(6) { [“post_type”]=> string(4) “post” [“posts_per_page”]=> int(4) [“tax_query”]=> array(1) { [0]=> array(5) { [“taxonomy”]=> string(8) “category” [“field”]=> string(7) “term_id” [“terms”]=> array(0) { } [“operator”]=> string(2) “IN” [“include_children”]=> bool(false) } } [“paged”]=> int(1) [“offset”]=> int(0) [“tax_term”]=> NULL }”