Skip Navigation

[Resolved] Display view content with The Grid Plugin

This support ticket is created 6 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 9 replies, has 2 voices.

Last updated by Nigel 6 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1123485

Hi, I use The Grid plugin hidden link
Elementor Pro and Toolset Type and View

I created a Custom Post Type with Toolset, called "Thematique" i set up a relationship beetween Thematique and Post. I created a "Single" Template on Elementor Pro for my CPT. Next I created a View who display all post related to my Thematique, and I add this view widget on my Elementor Single template.

Post is displaying correctly on each of my Thematique no problème with that.

What I want now is to use the grid plugin to display the result of the Loop created by View.

Can you help me to achieve that ? I try by using the function "use the grid as a template" but is not working properly.

PS : Sorry for my english, I'm French.

#1123667

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

There is no way to get The Grid and Views to work together so that you can have the output of a View styled by The Grid.

You have two options.

The first involves not using The Grid for anything other than inspiration. Design the output of your View so that it matches the styling of a grid. You could create a sample grid and then study the markup and styles used to create it, and then replicate that in Views.

The second involves not using Views.

Create a grid to display posts.

This will display all posts, and not just those which are related to the current thematique post.

I took a look at the documentation for The Grid, and they include an API filter to modify the query that would retrieve the posts to display.

hidden link

You could use this filter together with the Toolset relationships API to get all of the posts related to the current thematique, and set the post__in query argument to include only these post IDs.

You would use toolset_get_related_posts to retrieve the list of post IDs: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

If you get stuck let me know and I will see if I can help.

#1123694

Ok, Thank you for your advise.
I Think, I understand the two options.

I would like to know if I go for option n°2 using grid filter and relationship API.

Do I need to create 1 grid and put the grid on the page and i will automatically retrieve the good post.

Or do I need to create 1 grid for each "Thematique" with the good query ?

My goal is to have only one grid to manage so if i change the design of 1 grid it change for all page using this grid (exactly what I have done for archive page on my site). I don't want to have multiple grid for each of my CPT. Do you understand ?

#1123763

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Well, if I have understood you correctly

- you have a relationship between posts (standard WP posts) and Thematique CPT posts
- on the template to display a single thematique post you want to display the related WP posts in a grid

One would normally display these using a View which outputs posts and includes a Query Filter to specify the thematique parent (being the post where the View is displayed).

But you would like to use The Grid to output the blog posts instead (because, well, it looks very nice).

I didn't read too much of The Grid documentation, but I assume you create a Grid (to display standard WP posts in the style you want), and you insert this grid using a shortcode onto a page. Except you are not going to add it to a page, you are going to add it to the template you create for single Thematique posts using Elementor. (Which means I think you will be inserting the shortcode into a text module, unless there is some integration for adding Grids to Elementor.)

So when you visit a Thematique post on the front end, you will now see a grid of standard Posts.

The problem is that you are seeing *all* posts, and not just the ones that are related to the current Thematique post.

Which is where the solution I described above comes in, whereby you use the API filter to modify the query used by The Grid to return the list of posts. You will specify *which* posts using the post__in argument (https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters). You will generate that list using the Toolset relationships API, where you get the related Posts of the current Thematique post (which should be available as the global $post object).

#1123846

Ok I think if I read correctly is exactly that.

Now i need to add my argument on my functions.php can you help me to setup this ? (Please i'm bad in english, and not verry good at dev.) So if you can write all the cade it would be helpfull).

#1124303

Hi,

Ok I try something like this but is not working

function my_query_args($query_args, $grid_name) {

    if ($grid_name == 'Archive Grid for View') {
        // all query parameters can be modified (https://codex.wordpress.org/Class_Reference/WP_Query)
        $query_args['post_type'] = 'post'; {
			$post = toolset_get_related_posts(
    			$browser, // get posts related to this one
    			array( 'browser', 'post' ), // relationship between the posts
    			'parent', // get posts where $writer is the parent in given relationship
				'post_object'
);
		}
    }
    
    return $query_args;
    
}

add_filter('tg_wp_query_args', 'my_query_args', 10, 2);

But is not working I'm not sure about what I need to write on my query filter, please if you can help me.

Thank you.

*browser = slug of my CPT

#1124355

Ok sorry for posting multiple time, but i think after reading multiple time your answer, I understand what I need to do. So this is my code

function my_query_args($query_args, $grid_name) {

    if ($grid_name == 'Archive Grid for View') {
        // all query parameters can be modified (https://codex.wordpress.org/Class_Reference/WP_Query)
        $query_args['post__in'] = //Here I need to put un function that return ID of post that are related to the current CPT I'm in.
    }
    
    return $query_args;
    
}

add_filter('tg_wp_query_args', 'my_query_args', 10, 2);

Correct me if I'm wrong, I think I need to use the $return argument of Toolset Relationship API you provide above. But i don't know how to implement it if you can help me.

Thank You.

#1124376

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I don't have the plugin to be able to do any testing, but you are just missing using toolset_get_related_posts to get an array of related post ids that you can use to set the post__in query argument, which you would do like this:

function my_query_args($query_args, $grid_name) {
 
    if ($grid_name == 'Archive Grid for View') {

		global $post;
		error_log('$post: ' . print_r($post, true));

		$related_posts = toolset_get_related_posts( $post->ID, 'relationship-slug', 'parent', array( 'limit' => 999, 'return' => 'post_id', 'role_name_to_return' => 'other' ) );
		error_log('related_posts: ' . print_r($related_posts, true));

        $query_args['post__in'] = $related_posts;
    }
     
    return $query_args;
     
}
add_filter('tg_wp_query_args', 'my_query_args', 10, 2);

You need to edit the 'relationship-slug', which you can find at Toolset > Relationships, which for a relationship between 'browser' and 'post' types will likely be something like 'browser-post'.

I have left in a couple of error log messages. If the code doesn't work, this will help identify why (the global $post object not being the current thematique post the most likely reason).

If you haven't already, turn on the debug log by editing your wp-config.php file and change the line with WP_DEBUG like so:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

That will create a debug.log file in your /wp-content/ directory which you can examine in any text editor. The error_log messages will appear there.

#1127346

Hi,

Thank You for your help. Right now I put this on stanbye because I try other method you recomended using view.
If i need help I would let you know.

Thank You again.

#1127350

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

OK, well let me mark this as awaiting feedback from you so that it's not sitting in my queue, and if you have any more questions let me know.