Hi frontity community!
I want to randomize my CPT post loop, i create an array shuffle and working fine on first load, but if i reload page i’ve got a problem, the featured image doesn’t match the title, and in console i’ve got this error:
Prop `alt` did not match. Server: "name_of_my_media" Client: "name_of_my_other_media"
TBH I’m having a bit of difficulty trying to figure out what is going on here, esp without being able to see the structure of your data. It would be easier and more convenient to figure out what’s happening at each stage if I can clone your repo and access your data.
Can you provide a link to a repo please, and ensure that your data source is publicly available. This will enable both myself and others to be able to help you. Please see here for the kind of information about your issue that it’s useful to provide.
@mburridge if there’s a server/client mismatch error, presumably this is because the randomization is breaking the hydration process? I believe frontity does a server & client render conciliation and those 2 things should match?
if that’s the case it’s an interesting conundrum because the only way would be to return all items and then take a random selection, rather than doing a randomized query on the backend (since the randomization would not be the same on both server and client)
however returning all items on the API would presumably be far from optimal?
I believe @codemonkeynorth has a good point, for several reasons.
The server and client will both have different data since they both het a different list of random items, which will cause issues.
Second; it may a take a bit of PHP work to create a custom API endpoint, but performance wise it’s a lot better to have it limited and randomized at the server already. And I would ever advice you to do it at SQL level and not in PHP. So limit and randomize the results in the SQL query and return that in the REST API call.
Hi everyone! I’m not able to test until Tuesday, but maybe based on your suggestions this might be a good solution:
This article show how to randomize the post or custom post type post’s rest Api from wordpress php code, i think it’s a good solution, but i need to try first.