Filter post query based on term custom field

Support MB Views Filter post query based on term custom fieldResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #41391
    RoderickRoderick
    Participant

    Hi,

    I'm working on displaying a post grip of featured posts on a category archive page. The featured posts are set via a custom field inside the category, where I input the IDs of posts I want to show. Unfortunately, this does not query the selected post. When I manually add the post ID in the code it does work, so the general code itself works fine.

    How can I get the value from the term custom field 'term.featured_post_id' in order to query the correct posts? See my current code below, where I'm using: p: '{{ term.featured_post_id }}'

    {% set args = { post_type: 'post', posts_per_page: 6, order: 'DESC', p: '{{ term.featured_post_id }}' } %}
    {% set posts = mb.get_posts( args ) %}
    {% for post in posts %}
    	<div class="featured-article">
        <figure><img src="{{ post.thumbnail.large.url }}" width="{{ post.thumbnail.large.width }}" height="{{ post.thumbnail.large.height }}" alt="{{ post.thumbnail.large.alt }}"></figure>
        <h3>{{ post.title }}</h3>
        <a href="{{ post.url }}"></a>
        </div>
    {% endfor %}
    #41398
    PeterPeter
    Moderator

    Hello,

    The double open/close curly brackets are used to output the value on the frontend. If you assign the value to a variable, don't use it. For example:

    {% set args = { post_type: 'post', posts_per_page: 6, order: 'DESC', p: term.featured_post_id } %}
    
    #41400
    RoderickRoderick
    Participant

    Works like a charm! Thank you very much Peter.

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