0

I'm querying an ACF field, and displaying all the results on the front-end like so—
PHP:

            <ul class="category-menu-items">
                <?php 

                // args
                $args = array(
                    'numberposts' => -1,
                    'post_type' => 'wpsc-product'
                );

                // get results
                $the_query = new WP_Query( $args );

                // The Loop
                ?>
                <?php if( $the_query->have_posts() ): ?>
                    <ul class="inside-category-menu-items">
                    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                        <li class="category-menu-item">
                            <a href="/shop/?view_type=default&product_search=<?php $remove = array(" ", ",", "/", ".", ":", "-", "–", "—", "!", "?", ";");
echo strtolower(str_replace($remove, "+", get_field('products_page_designer_name'))); ?>">
                                <?php the_field('products_page_designer_name'); ?>
                            </a>
                        </li>
                    <?php endwhile; ?>
                    </ul>
                <?php endif; ?>

                <?php wp_reset_query(); ?>
            </ul>

This works great, the only problem is the values of some of the fields are duplicates of others, e.g. It will output a list like so:

Value 01
Value 02
Value 02
Value 03
Value 03

etc etc. but I only want to display one instance of each, thus if there's a duplicate value it will be hidden. Is this at all possible? Any suggestions would be greatly appreciated!

closed as off-topic by kaiser May 16 '14 at 13:15

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

3

You could pass all the fields into an array during your while loop, strip the array of duplicates, and then run a foreach loop on that array. It would look something like this:

<?php 
$designers = array();
while ( $the_query->have_posts() ) : $the_query->the_post();
     $designers[] = get_field('products_page_designer_name');
endwhile;

// This will strip out any values that are identical.
$designers = array_unique( $designers );

// Run through your unique array of designers
foreach ( $designers as $designer ) { ?>
    <li class="category-menu-item">
        <a href="/shop/?view_type=default&product_search=<?php
            $remove = array(" ", ",","/", ".", ":", "-", "–", "—", "!", "?", ";");
            echo strtolower(str_replace($remove, "+", $designer)); ?>">
            <?php echo $designer; ?>
        </a>
    </li>
<?php } ?>

Note that I haven't tested the syntax or the code.

Good luck!

1

You can use get_field_object function to get all field that been create for a post and generate by ACF.

Here is code to get all custom fields from group id:

<?php
$groupID='23';
$custom_field_keys = get_post_custom_keys($groupID);
foreach ( $custom_field_keys as $key => $fieldkey )
{
    if (stristr($fieldkey,'field_'))
        {
            // get_field_object( $field_name, $post_id, $options )
        // - $value has already been loaded for us, no point to load it again in the get_field_object function
        $field = get_field_object($fieldkey, $groupID);

        echo '';
            echo '<span><span>' . $field['label'] . '</span>:';
            echo ''. get_field($field['name']) . '</span>';
        echo '';
     }

}
?>
0

EDIT: They will always return the same value if they have the same field name

get_field('field_name1') and the_field('field_name1') are the same value if you use the same field_name!

Take a look at the docs for get_field and the_field.

For the_field() it says:

this is the same as “echo get_field($field_name)”


One way to do so would be to put then in an array and then check to see if they are in the array using in_array().

I would instead use a class or function to build an object or an array with the correct values and then supply it to the template with the preformed values. Keep the logic out of your template/view!

MVC Principles

Here is a presentation about MVC in Wordpress by Ian Dunn.

Essentially what you should want to do is eliminate code reuse as much as possible. That is what MVC tries to accomplish as well as keeping everything in a logical structure.

Basically what he outlines is this:

MVC: Controllers

  • Direct traffic within the application
  • Collect data from models, pass to views
  • Doesn't maniuplate the data

MVC: Models

  • Data layer
  • Handles business/domain logic
  • Does the heavy lifting

MVC: Views

  • Only the presentation of the data - HTML/CSS
  • Use inline PHP, but sparingly
  • No data manipulation

Fat Models, Skinny Controllers

For example:

function get_menu_items_object() {
    $menu_items = array();
    $add_to_menu_items = array(
    'designer_name1' => get_field('products_page_designer_name'),
    'designer_name2' => the_field('products_page_designer_name')
    );
   foreach($add_to_menu_items as $item_key => $item) {
        if (!in_array($item, $menu_items)){
        $menu_items[$item_key] = $item;
    } else {
        $menu_items[$item_key] = '';
    }
    return (object) $menu_items;
}

Then all you would have to do is:

$menu_items_object = get_menu_items_object();
echo $menu_items_object->designer_name1; //value echoed
echo $menu_items_object->designer_name2; //value echoed.  If '' then it is nothing

Not the answer you're looking for? Browse other questions tagged or ask your own question.