0

I am trying to create a shortcode called [brand_collection] with the attributes brand and collection. The shortcode with create a table from a custom post type with ACF on it. here is my ACF setup [ACF setup[1]

ACF repeater setup

and with an example of how a finished post looks. post example

there are several collection for each brand. Right now I have 139 collections with more coming. before I was able to get this code to work.

function collections_shortcode($atts)
{

    // Parse your shortcode settings with it's defaults
    $atts = shortcode_atts(array(
        'brand' => '',
        'collection' => '',
    ), $atts);

    // Extract shortcode atributes
    extract($atts);

    // Define output var
    $output = '';

    // Define query
    $query_args = array(
        'post_type' => 'framecollection', // Change this to the type of post you want to show
        'posts_per_page' => '1',
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'brand',
                'value' => $atts['brand'],
                'compare' => '=',
            ),
            array(
                'key' => 'collection_number',
                'value' => $atts['collection'],
                'compare' => '=',
            ),
        ),
    );

    // Query posts
    $custom_query = get_posts($query_args);

    // Add content if we found posts via our query
    if ($custom_query->have_posts()) {

        // Open div wrapper around loop
        $output .= '<table class="keplrTable">';
        $output .= '<thead>';
        $output .= '<tr>';
        $output .= '<th>Image</th>';
        $output .= '<th>Style</th>';
        $output .= '<th>Color Code</th>';
        $output .= '<th>Color</th>';
        $output .= '<th>Size</th>';
        $output .= '<th> Wholesale Cost </th>';
        $output .= '<th>UPC</th>';
        $output .= '</tr>';
        $output .= '</thead>';
        $output .= '<tbody>';

        // Loop through posts
        while ($custom_query->have_posts()) {
            $custom_query->the_post();

            if (have_rows('collection_info')) {

                while (have_rows('collection_info')) {
                    the_row();
                    $image = get_sub_field('image');
                    $style = get_sub_field('style');
                    $color_code = get_sub_field('color_code');
                    $color = get_sub_field('color');
                    $size = get_sub_field('size');
                    $wholesale_cost = get_sub_field('wholesale_cost');
                    $upc = get_sub_field('upc');

                    $output .= '<tr>';
                    $output .= '<td><img src="' . $image['url'] . '" alt="' . $atts['brand'] . ' Frames"/></td>';
                    $output .= '<td>' . $style . '</td>';
                    $output .= '<td>' . $color_code . '</td>';
                    $output .= '<td>' . $color . '</td>';
                    $output .= '<td>' . $size . '</td>';
                    $output .= '<td>$' . $wholesale_cost . '</td>';
                    $output .= '<td>' . $upc . '</td>';
                    $output .= '</tr>';

                }

            }
        }

        $output .= '</tbody>';
        $output .= '</table>';

        // Restore data
        wp_reset_postdata();

    }
    return $output;

}
add_shortcode('brand_collection', 'collections_shortcode');

to create a shortcode like this [brand_collection brand="Versace" collection="7"] which would output the table like this, just more cleaned up looking.

table output

The purpose of these shortcodes is for a multipage form built in gravity forms. As they click checkboxes that say "Collection 1, Collection 2,..." the table will appear on the right showing what is in each collection. As you can imagine doing a WP_Query for each shortcode displayed for over 130 tables plus several users going to the form at the same time can be a major resource hog.

So after that long-winded backstory. What would be a more efficient way to call ACF fields from a CPT, to create a shortcode that is not on the CPT page?

I've looked around for the past few days and I keep finding solutions that are either the same way I am doing it, but in much smaller amount or to call the fields directly but usually the ACF is on the CPT page.

TL;DR How would I call ACF fields from a CPT, to create a shortcode that is not on the CPT page?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.