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.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.