Support

Account

Home Forums Add-ons Repeater Field Get Term ID from repeater sub field and Options page

Solved

Get Term ID from repeater sub field and Options page

  • I’m using Repeater Field and Options page add-ons.
    I’m building an option panel where the user can choose the category of posts displaying in the homepage.

    So, I created a repeater field with position one, position two, etc
    Each position of the repeater contains a taxonomy field which extracts categories of posts and let the user choose the one preferred via select HTML.
    The value which should be returned is “Term ID”.

    In WP template I’d like to use the term ID as a variable for a get_posts query.

    This is the code (where “priorita_homepage” is the repeater field and “prima_posizione” is a sub field)

    if(get_field('priorita_homepage'))
    						{
    							while(has_sub_field('priorita_homepage'))
    							{
    								$categoria = the_sub_field('prima_posizione');
    							}
    						}
    
    						$args = array(
    							'posts_per_page'   => 1,
    							'category'         => $categoria,
    							'orderby'          => 'post_date',
    etc...

    But the term ID is empty ($categoria) is empty, so I don’t get any results. What am I missing?

    • Elliot

    • November 19, 2013 at 5:33 pm

    Hi @digitalfaber

    There are 2 issue with your code that I can see:
    1. You are using the_sub_field, when you should be using get_sub_field
    2. Your $args are OUTSIDE the has_sub_field loop so your WP_Query or get_posts call will only happen once, not for each row in the repeater field

    Thanks
    E

  • Hi Elliot, thank you for your reply.

    I finally realized that my approach was totally wrong:
    – I didn’t need a repeater field, as positions are defined. Instead, for UI needs, I’ll go for the Flexible Layout Addon, the one still missing to my collection 😀
    – I was missing the ‘option’ parameter between get_field brackets

    Now everything is working as expected, I marked topic as resolved.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.