Hi, I hope you can help.
I built my original site back in 2020 using layouts and views. I'm now working on a staging site to update the sign to a more modern design. I'm having some issues with the new blocks when trying to call related products. For context, we are a book publisher, so on the book (product) pages we want to have 'books by this author'.
We have custom post types for 'authors', and the way we did this on the original site was to add a view for 'related books', and in the query filters we queried the field 'Author' with shortcode attribute 'author'. The output worked.
On the new site, a staging site using duplicated files, the same exact view (and recreated by building a new one using the same logic), outputs all products rather than just the ones by the same author. I've done some extensive Googling and followed advice to add the view in the shortcode block instead; this outputs the same (ie all) products, but with the style stripped out!
Hope you can help!
Old site, working: enlace oculto
New site, not working: enlace oculto
Hi,
Welcome to WPML support. The behavior you're seeing points to the filter having no value to work with rather than the filter being broken. A Query Filter set to value set by View shortcode attribute gets that value only from the attribute written into the View's shortcode, for example:
[wpv-view name="related-books" author="123"]
When the View is inserted with the View block instead, no shortcode attribute is passed, so the filter has nothing to match on and the query returns everything of that post type.
You can learn more here:
https://toolset.com/course-lesson/filtering-custom-lists-of-posts/
So the question is how the value reached that attribute on your original site. On a Layouts/legacy build the View shortcode would have had the author value written into it dynamically, something like author="[types field='author'][/types]", evaluated for the current book.
To give you the exact fix rather than a guess, could you tell me:
1. How are books linked to authors? A Types post relationship, a post reference field, a plain custom field holding the author's ID or name, or a taxonomy? This decides which filter you should use.
2. What does the working shortcode on the old site look like in full, including the author="..." part? If you can open that page/layout in the old site's editor and copy the whole [wpv-view ...] line, that tells us how the value was being generated.
3. Which Query Filter type is selected in the new View — post relationship, custom field, or something else — and what is the attribute name set to?
Thanks.
Many thanks for your quick reply! You're quite right, my mistake – sorry for messing you about. Adding [types field…] information to the shortcode fixed it.
In shortcode format the view was still not rendering well visually – rather than having, say, five other books side by side (as I had got them designed to look good in the view), the shortcode version rendered them full size, whole screen; I played around with it a bit and it seems to be that when returned as a shortcode the columns and alignment is stripped. So I've rejigged the format so it looks better now, and rather than opting for five books side by side, there is instead information on just one extra book at a time – but now a new problem has come up! While the filter now seems to work and produces pagination for the right amount of extra books by each author, if you click on the 'next page' blob in the pagination, it thinks about it and then the whole view disappears! This is slightly difficult to explain, so I'm pasting below a link to the staging site on an example page, and hopefully you can recreate the issue on your end and be able to point me in the right direction…
enlace oculto
Hi,
Thank you for the explanation. Unfortunately, there is no built-in solution for this because the attribute containing the data is added when the page loads.
However, the pagination you are using is AJAX-based, and it is not possible to carry this data through AJAX using JavaScript.
It may be possible with extensive custom JavaScript, PHP, and REST API development, but that is well outside our support scope.
I suggest changing the pagination type in the View from AJAX pagination to the page reload option.
Thanks.