How to get a post by it's slug

Support MB Views How to get a post by it's slugResolved

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #43966
    EddyPiVEddyPiV
    Participant

    Hi,

    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,
    Eddy

    #43967
    EddyPiVEddyPiV
    Participant

    Just 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.

    #43972
    PeterPeter
    Moderator

    Hello,

    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 } %}
    
    #43980
    EddyPiVEddyPiV
    Participant

    Hi 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?

    #43987
    PeterPeter
    Moderator

    Hello,

    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 %}
    #43994
    EddyPiVEddyPiV
    Participant

    Thanks Peter, all fine now.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.