How to get a post by it's slug
- This topic has 5 replies, 2 voices, and was last updated 1 year, 5 months ago by
EddyPiV.
-
AuthorPosts
-
November 30, 2023 at 5:17 AM #43966
EddyPiV
ParticipantHi,
I am showing a list of fans, but for some reason the amount spent by the fan is not shown.
There are cpt's for Fan and FanSpending. In the Fan custom fields there is a field 'fan_profile' which is the key (title + slug) to FanSpending.
There is a test Fan with an existing FanSpending, but it's not shown:
https://www.imgbly.com/ib/lQO02ysOhe{% set args = { post_type: 'fan', posts_per_page: -1, orderby: 'title', order: 'asc' } %} {% set posts = mb.get_posts( args ) %} <table> <tbody> <tr> <th><strong>Fan ID</strong></td> <th><strong>Name</strong></td> <th><strong>Profile</strong></td> <th><strong>Spent</strong></td> <th><strong>Country</strong></td> <th><strong>Action</strong></td> </tr> {% for post in posts %} <tr> <td>{{ post.title }}</a></td> <td>{{ post.name }}</td> <td>{{ post.fan_profile }}</td> <td>{% set spending_args = { post_type: 'fanspending', name: '{{ post.fan_profile }}', numberposts: 1 } %} {% set spending_posts = mb.get_posts( spending_args ) %} {% for spending_post in spending_posts %} {{ spending_post.amount }} {% endfor %} </td> <td>{% set country_args = { post_type: 'country', relationship: { id: 'fan-countrydetails', from: post.ID } } %} {% set country_posts = mb.get_posts( country_args ) %} {% for country_post in country_posts %} {{ country_post.post_title }} {% endfor %}</td> <td class="middle"><a href="https://domain.com/fan-details/?rwmb_frontend_field_post_id={{ post.ID }}" rel="">[icon name="pencil-alt" style="solid" class="middle2" unprefixed_class="middle2"]</a> <a href="https://domain.com/fan/{{ post.slug }}" rel="">[icon name="eye" style="regular" class="middle2" unprefixed_class="middle2"]</a></td> </tr> {% endfor %} </tbody> </table>
I'm pretty sure it's a small detail that I'm missing, but can't figure it out.
Can you pls comment?
Thanks, regards,
EddyNovember 30, 2023 at 5:22 AM #43967EddyPiV
ParticipantJust to add, to demonstrate that {{ spending_post.amount }} is correct: I tested by taking out
name: '{{ post.fan_profile }}'
, and the amount was shown for each fan.November 30, 2023 at 5:36 PM #43972Peter
ModeratorHello,
Using the double curly brackets to output the value, don't use them when you use a variable.
{% set spending_args = { post_type: 'fanspending', name: post.fan_profile, numberposts: 1 } %}
December 1, 2023 at 9:24 PM #43980EddyPiV
ParticipantHi Peter, thanks, that almost works.
It just, if post.fan_profile is empty, then the value of the most recent occurrence is returned.
How to avoid that?December 3, 2023 at 8:52 PM #43987Peter
ModeratorHello,
You can check the fan_profile is not empty before setting the args. For example:
{% if post.fan_profile is not empty %} {% set spending_args = { post_type: 'fanspending', name: post.fan_profile, numberposts: 1 } %} ... {% endif %}
December 3, 2023 at 11:48 PM #43994EddyPiV
ParticipantThanks Peter, all fine now.
-
AuthorPosts
- You must be logged in to reply to this topic.