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.
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.
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 ?
- 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).
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).
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.
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.
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:
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: