Support

Account

Home Forums Front-end Issues Custom fields from custom types through the location rules

Solving

Custom fields from custom types through the location rules

  • Hi, thank you for your plugin. It’s Fantastic!
    I have a problem with the location rules.
    I have one custom post type. I have configured two custom fields. one for normal wordpress post, and another field for my custom post type via the location rules. In the backend editor all work fine!
    Now i want use the value of field from custom type (with a new wp-query loop) in single.php (loop of normal wordpress posts) but the get_field function don’t return any values.
    I think because the field that i want use in single loop is set to location of custom type…
    How i can merge all fields in single loop for use in variables or query?
    Thank you so much!

  • Hi @pastore1980

    Your question is quite hard to understand. Can you please provide a clearer explanation of what you are trying to do?

    Please name your CPT instead of saying I have one custom post type.

    Also, please refrain from such broad and vague terminology like this:
    Now i want use the value of field from custom type (with a new wp-query loop) in single.php

    I can tell you that the get_field function can be used WITHIN a custom WP_Query loop.

    What code are you using? Perhaps you can post it (please wrap in backticks – code button)

  • Hi, thank you for your response..
    I tell you what you ask me..
    My custom post type is ‘pacchetto’ and i have two group for each post type. One for the wordpress posts: Articoli and one for my custom post type: pacchetti
    I want a query with CPT’s posts ‘pacchetto’ in sigle.php where return the posts when fields ‘zona_per_gli articoli’ = ‘zona-per-i-pacchetti’
    I use the follow code:

    <?php
    $zone_articoli = get_field('zona_per_i_pacchetti');
    $args = array(                      
            'post_type' => 'pacchetto',                                   
            'meta_query' => array(
                    'key' => 'zona_per_gli_articoli',
                    'value' => $zone_articoli
                );
    $loop = new WP_Query($args);
    while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <div> 
    <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    </div>
    <?php endwhile; wp_reset_postdata();?>

    This code return me all posts from CPT ‘pacchetto’ and not filtering with fields. I think because i am in single.php and there are a rules for each post type
    In effect if i write
    echo $zone_articoli it return the effective value, but if i write

    $zone_pacchetti = get_field('zona_per_gli_articoli');
    echo $zone_pacchetti; 

    don’t work (no value).
    This last code work in single-pacchetto.php
    I think i must merge the location rules for make my query with my fields…
    Thank you so much!

  • Hi @pastore1980

    To clarify:
    1. The above code is used int he single.php template (view for a post)
    2. The filter value is loaded via $zone_articoli = get_field('zona_per_i_pacchetti');
    3. The $loop find all pacchetto and ignores the filter

    Perhaps you need to debug your code. Try removing the $zone_articoli from the equation and hard code the meta_query.

    Also, is the select field a single or multi select?

    Thanks
    E

  • Hi, thank you for your support.
    For your questions:
    1) yes
    2) yes
    3) yes

    However, this code works!

    <?php
    $zone_articoli = get_field('zona_per_i_pacchetti');
    $args = array(
      'post_type'=>'pacchetto',
      'meta_key'=>'zona_per_gli_articoli',
      'meta_value'=>$zone_articoli
    );
    $loop = new WP_Query($args);
    while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    <div> 
    <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    </div>
                        <?php endwhile; wp_reset_postdata();?>

    A last question:
    If i want use a multiple select for a field, how i can do it?
    The field that i want use is ‘zona_per_gli_articoli’
    Thank you so much for your help!

  • Hi @pastore1980

    You can perform a meta_query on a ‘multi-value’ field by using a LIKE parameter.
    Here is an example (last example on page):
    http://www.advancedcustomfields.com/resources/field-types/checkbox/

    Thanks
    E

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

The topic ‘Custom fields from custom types through the location rules’ is closed to new replies.