skip to Main Content

I am building a website using Advanced Custom Fields that has yachts for sale. The yachts use a custom Post type called ‘yachts’

One of the fields in the individual yacht listing is a true/false value to determine wether it should be a featured listing. (it was originally a checkbox but I changed it after experimenting with the answer in this post)

Advanced custom fields: can't query posts by custom field

I am trying to display previews of the featured listings on the home page but I can’t get it to work.

I originally tried this code from the ACF documentation

edit: I started with just trying to fetch the title but I also need the additional fields

<section class="featured-yachts">
    
    <h2>FEATURED YACHTS</h2>
    
    <?php 
    
    // args
    $args = array(
        'numberposts'   => -1,
        'post_type'     => 'yachts',
        'meta_key'      => 'featured',
        'meta_value'    => 'yes'
    );
    
    
    // query
    $the_query = new WP_Query( $args );
    
    ?>
    <?php if( $the_query->have_posts() ): ?>
        <ul>
        <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <li>
                <a href="<?php the_permalink(); ?>">
                    <?php the_title(); ?>
                </a>
            </li>
        <?php endwhile; ?>
        </ul>
    <?php endif; ?>
    
    <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>
    
    
    
</section>  

And now have this after switching to a true false field

<section class="featured-yachts">
    
    <h2>FEATURED YACHTS</h2>
    
    <?php 
    
    $args = array(
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'featured',
                'value' => '1',
                'compare' => 'LIKE',
            )
        ),
    );

    $my_posts = new WP_Query($args);

    if ($my_posts->have_posts()) {

        while ($my_posts->have_posts()) : $my_posts->the_post();

          echo get_the_title();

        endwhile;
    wp_reset_postdata();

}
    
    ?>
    
</section>  

I have also tried editing this: How filter custom posts by Advanced Custom Fields checkbox

but again, I cant get it to work.

The most any of these return is the h3 title

I can’t work out what the issue is.

Thanks in advance for any help

edit : I have at least one post using the custom post type ‘yachts’ that is set to true for featured.

I still have lots of fields to add, but I would have expected that this output the h3 title and then the title of the post marked featured.

In the end, I would like it to function much like latest post previews, but display custom fields and only if the ‘featured’ true/false is set to yes

I have made an ACF field called ‘featured yachts’ and registered the block with Gutenberg, but there are no actual ACF fields within it, it’s only used to call the file ‘content-featured-yachts-block.php’ which has this code I am trying to fix.

I am trying to populate the post previews within this featured yachts block with data pulled from the individual yacht listings. In these listings is the ‘featured’ true/false option. Screen shot atttached.

enter image description here

This is my code for registering the CPT

// Our custom post type function
function create_posttype() {
 
    register_post_type( 'yachts',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Yacht Listings' ),
                'singular_name' => __( 'Yacht' ),
                'supports' => array( 'title', 'editor', 'comments', 'excerpt', 'custom-fields', 'thumbnail' ),
                'add_new' => __( 'New Yacht Listing'),
                 'add_new_item' => __( 'Add New Yacht Listing'),
                 'edit_item' => __( 'Edit Yacht Listing'),
                 'new_item' => __( 'New Yacht Listing'),
                 'view_item' => __( 'View Listings'),
                 'search_items' => __( 'Search Listings'),
                 'not_found' =>  __( 'No Yacht Listings Found'),
                 'not_found_in_trash' => __( 'No yacht Listings found in Trash')
            ),
            'public' => true,
            // 'has_archive' => true,
            'rewrite' => array('slug' => 'yachts'),
            'show_in_rest' => true,
            'menu_icon' => 'dashicons-sos',
            'hierarchical' => true,
            'taxonomies' => array('category')
        )
    );
}

Another edit – I made the assumption that if I could get the title field to show, the other fields would be easy but i can’t get them either so I ALSO need to work out how to add the additional fields from the listing

The code I have tried is below

<h2>FEATURED YACHTS</h2>

<?php 

$args = array(
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => 'featured',
            'value' => 'yes',
            'compare' => 'LIKE',
        )
    ),
);

$my_posts = new WP_Query($args);

if ($my_posts->have_posts()) {

    while ($my_posts->have_posts()) : $my_posts->the_post();?>

    <h2><?php the_title(); ?></h2>
    
    <?php
    get_post_meta( get_the_ID(), 'price', true );
    ?>

    <?php endwhile;
wp_reset_postdata();

}
?>

AND ALSO

<section class="featured-yachts">

    <h2>FEATURED YACHTS</h2>

    <?php 

    $args = array(
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'featured',
                'value' => 'yes',
                'compare' => 'LIKE',
            )
        ),
    );

    $my_posts = new WP_Query($args);

    if ($my_posts->have_posts()) {

        while ($my_posts->have_posts()) : $my_posts->the_post();?>

        <h2><?php the_title(); ?></h2>
        
        <p><?php the_field( 'price' ); ?></p>


        <?php endwhile;
    wp_reset_postdata();

}

    ?>

</section> 

2

Answers


  1. You can use ‘meta_query’ and for ACF true and false you don’t have to compare with LIKE. Try the below code.

    <section class="featured-yachts">
    
        <h2>FEATURED YACHTS</h2>
        
        <?php 
        
        $args = array(
            'post_type' => 'yachts',
            'posts_per_page' => -1,
            'meta_query' => array(
                array(
                    'key' => 'featuredyacht',
                    'value' => '1',
                )
            ),
        );
    
        $my_posts = new WP_Query($args);
    
        if ( $my_posts->have_posts() ) {
    
            while ( $my_posts->have_posts() ) : $my_posts->the_post();
    
                echo "Title - ".get_the_title()."</br>";
                echo "Price - ".get_field( "price", get_the_ID() )."</br>";
                echo "Length - ".get_field( "length", get_the_ID() )."</br>";
                echo "Year built - ".get_field( "year_built", get_the_ID() )."</br>";
                echo "Capacity - ".get_field( "capacity", get_the_ID() )."</br>";
    
            endwhile;
    
            wp_reset_postdata();
    
        }
        
        ?>
        
    </section> 
    
    Login or Signup to reply.
  2. To use multiple custom field parameters in your query, replace $args as in Bhautik‘s answer with something like this:

    $args = array(
        'post_type' => 'yachts',
        'posts_per_page' => -1,
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'featuredyacht',
                'value' => '1',
            ),
            array(
                'key' => 'price',
                'value' => 50000,
                'type' => 'numeric',
                'compare' => '>=',
            )
        ),
    );
    

    The above example fetches only yachts with featuredyacht set to 1 or true AND with a price of 50,000 or more. Adjust as needed to suit your parameters.

    For example, to query featured yachts within a specific price range:

    $args = array(
        'post_type' => 'yachts',
        'posts_per_page' => -1,
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'featuredyacht',
                'value' => '1',
            ),
            array(
                'key' => 'price',
                'value' => array( 50000, 100000 ),
                'type' => 'numeric',
                'compare' => 'BETWEEN',
            )
        ),
    );
    

    The developer documentation on WP_Query should be very useful to you for any further customizations.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search