Support

Account

Home Forums General Issues Help retrieving with ACF to define posts shown – Using a Shortcode

Solving

Help retrieving with ACF to define posts shown – Using a Shortcode

  • 0

    I have created what I think will pull posts with the custom field values for ‘bustype’ – when the value is then entered into the shortcode as a parameter. This code was working fine before I tried to add the custom field.

    I have tried this, however, and nothing is displaying as posts.

    From looking at what I have written below, and knowing that the Advanced Custom Field is a select field with set values ‘buyers’, ‘service’, and ‘otherbus’ – is there any reason what I have below would not check against the value in metavalue=”” entered within the shortcode?

    Shortcode I used, to display posts with the value ‘buyer’:

    [list-posts type="company_directory" posts_per_page="40" posts="40" metavalue="buyer"]

    Below is the code I have added to functions.php to try and use the custom field.

    // start code to add shortcode
    
    add_shortcode( 'list-posts', 'rmcc_post_listing_parameters_shortcode' );
    function rmcc_post_listing_parameters_shortcode( $atts ) {
    ob_start();
    
    // define attributes and their defaults
    extract( shortcode_atts( array (
        'type' => 'post',
        'order' => 'ASC',
        'orderby' => 'menu_order',
        'posts' => -1,
        'color' => '',
        'class' => 'post',
        'fabric' => '',
        'category' => '',
        'metavalue' => '',
        'fieldname' => 'bustype',
    ), $atts ) );
    
    // define query parameters based on attributes
    $options = array(
        'post_type' => $type,
        'order' => $order,
        'orderby' => $orderby,
        'posts_per_page' => $posts,
        'color' => $color,
        'class' => $class,
        'fabric' => $fabric,
        'category_name' => $category,
        'meta_key' => $fieldname,
        'meta_value' => $metavalue,
    );
    
    $query = new WP_Query( $options );
    // run the loop based on the query
    if ( $query->have_posts() ) { ?>
    
        <?php $i = 0; ?>
        <div class="bus-directory et_pb_row et_pb_row_1 et_pb_gutters2 et_pb_row_4col"> 
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>
    
    // Content to return goes here
    
        <?php $i++; endwhile; ?>
    <?php
        $myvariable = ob_get_clean();
        return $myvariable;
    }
    }

    Thanks for your help.

  • I really just need to know the problem with the reference to the actual field and the way I am checking the value? Thanks

  • If the field bustype is a select field then it stores an array of values and you must do a "LIKE" meta query.

    
    ....
    'meta_value' => $metavalue,
    'meta_compare' => 'LIKE'
    ....
    

    This would be event better

    
    ....
    'meta_value' => '"'.$metavalue.'"',
    'meta_compare' => 'LIKE'
    ....
    

    For more information on querying by different types of fields see https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

  • Thanks for the help John.

    I am using a select field but only one value at a time? It would not contain more than one value at once – is that still a problem?

  • Added that to the code John but still not producing any results sadly. Not sure what I’m doing wrong as the shortcode works great before I added the ACF elements.

  • Not really sure, but what are these values

    
    
        'color' => $color,
        'class' => $class,
        'fabric' => $fabric,
        'category_name' => $category,
    

    category_name specifically seems to be empty. I’m not sure what happens if you include this query parameter and do not include a value. Is it ignored or will it only get posts that have no category? There is nothing in the WP_Query docs that say this can be empty. Empty values can have odd results.

  • When I take out the ACF bit, the shortcode works great John, so that category part has no problems. The issue seems to be 100% with how I am passing the ACF values to the shortcode.

    The reason it’s blank is in creating the shortcode parameter itself to allow the option to specify a category if desired. When I leave that blank it just shows all posts 🙂

    The problem is now I have added ACF, when I am trying to use the newly created parameter ‘metavalue’ in my shortcode, it doesn’t show any results.

    I could use categories for this but it ‘seemed’ easier to use ACF and now I am keen to know why this isn’t working for future 🙂

  • Also sorry – those are just placeholders and not used, they basically loop into nothing unless used in the shortcode itself and then they would still return null.

  • With all the above, I don’t see any reason that your query should not be working.

    Unless ‘bustype’ is a sub field of a repeater?

  • You said it’s a select field, is it a “Select” field or some type of relationship field?

  • http://recordit.co/gQdsTUBI9E

  • Hi John – this is my field

    http://recordit.co/gQdsTUBI9E

  • Looking closer at your code, I actually can’t see that this would output anything, with or without ACF.

    I don’t see global $post and with that said you also need to call wp_reset_postdata() after your while loop.

  • Hi John

    Is there another way I can demo this to test the fields? I also can’t see a reason the fields would not work.

    The code works 100% when the ACF parameters are not set – I can use the shortcode with the custom post type specified and it returns the correct information just fine.

    [list-posts type="company_directory" posts_per_page="40" posts="40"]

    This displays all the posts of the type company directory.

    I just then wanted to query what is shown based on the field option within that post type?

    I based this code from an article online about creating flexible shortcodes in WordPress.

  • The post I referenced was: https://code.tutsplus.com/tutorials/create-a-shortcode-to-list-posts-with-multiple-parameters–wp-32199

    Under the title
    Adding Some Parameters to Modify the Query

    Like I say, this code did work fine for me, I just can’t get ACF to work with it.

  • Hi John

    I also tried using the simple select value example on the docs too, and that returned absolutely nothing too, which is a bit bizzare.

    I found another post from you on this forum relating to select fields, so tried the following for my code:

        $options = array(
            'post_type' => $type,
            'order' => $order,
            'orderby' => $orderby,
            'posts_per_page' => $posts,
            'color' => $color,
    		'class' => $class,
            'fabric' => $fabric,
            'category_name' => $category,
    		'meta_query'	=> array(
    		'relation'		=> 'OR',
    		array(
    			'key'		=> 'bustype',
    			'value'		=> 'buyer',
    			'compare'	=> 'LIKE'
    		),
    		array(
    			'key'		=> 'bustype',
    			'value'		=> 'service',
    			'compare'	=> 'LIKE'
    		),
    		array(
    			'key'		=> 'bustype',
    			'value'		=> 'otherbus',
    			'compare'	=> 'LIKE'
    		)
    	)

    Something is now returned, but it’s just 4 posts of the 10 I have live, and they are not related to any of the above ACF really as the results are mixed. The above contains all the select values, so all the posts should be shown technically.

    It does show though that the code somewhere along the line for ACF I am using is not finding or producing the right results.

  • Hi John, I’ve tried using a shortcodes plugin too and that works well for everything apart from when I use the parameters in this field.

    I am a bit baffled why the fields seem to be causing problems – is there something I might be missing here? The values seem to be coming back as nothing.

  • Changing it to a radio button field worked. Why does select not work?

  • Sorry for no reply. I haven’t thought of anything useful to help you or why the code is not working.

  • Thanks John, very odd. The Radio field made it work well, but I think the loop you are correct in saying it isn’t closing properly.

  • Adding

    wp_reset_postdata();

    Solved the loop bug I had going on on those pages John, thank you mate.

    Where did you think the global $post was missing?

    I called

    $query->have_posts() ) : $query->the_post();

  • When you call have_posts() it assigns the post data to the global $post variable. It doesn’t matter if his is the main WP query or your custom query. This will only be an issue if you are accessing post information with functions like the_title().

  • That does make sense John.

    Oddly however, I am indeed calling the_title() and it’s returning just fine? :/

Viewing 23 posts - 1 through 23 (of 23 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.