Custom Query to display posts with custom field

I am running into an issue with a custom query for displaying my posts and am having no luck on the WordPress Forum nor the plugin forum I used to create the Custom Post Type.

Here is an overview…

I have a Custom Post Type “Business”
Business CPT has two Custom Taxonomies — “Business Type” & “County”

I used WP Types plugin to create the Custom Post Type and create a custom field called “Featured” using a checkbox in the admin area.

This checkbox if selected marks a particular entry as “FEATURED”

I am using a plugin called Search&Filter which allows a user to pick a “Business Type” in “County” EG – CAR HIRE in LONDON

This then runs my search.php template with a query which SHOULD…

  1. Display any “FEATURED” businesses with both Car Hire and London Taxonomies as true.
  2. Then display the remaining business listing that do not have “Featured” set to true.

So

FEATURED BUSINESS

Daves Car

Remaining Businesses

ABC Cars

123 Cars

etc etc.

At the moment Car Hire has 5 Featured businesses. My query is pulling them all out but not all at the top. It is just adding them in the alphabetical list with the regular business listings.

Its driving me mad and hopefully somebody far greater than me at PHP and WordPress can help me sort this out.

Here is the query I am currently using

<?php   
    if (have_posts()): while (have_posts()) : the_post(); 
        $checked_meta = get_post_meta( $post->ID, 'wpcf-featured', true );

    if( $checked_meta ) { ?>   
    <article>
      <!--FEATURED BUSINESSES DISPLAY FIRST-->
    </article>
<?php } endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata();?>

<hr class="bottom-padding" />

<?php 
    $posts = query_posts($query_string . '&orderby=title&order=asc'); 
    if (have_posts()) : while (have_posts()) : the_post();?>

        <article class="row business-index" id="post-<?php the_ID(); ?>">
            <!--Normal Business Listings--> 
        </article>      
    <?php endwhile; ?>
    <?php endif; ?>

All I want to do is display all “FEATURED” businesses for that particular search FIRST then display the remaining listings.

I am at the end of a very short piece of rope with this now. Hope somebody can save my mind. Hahaha

Thanks

Dan

1 Answer
1

As @jdm2112 implied, WP_Query for your 2nd display is preferred over query_posts. Try using WP Query for your first display as well.

It would look something like:

<?php  $args = array(
    'post_type'   => 'cars', // or whatevr the custom post type is 
    'post_status' => 'publish',

    'meta_query'     => array(
        array(
            'key' => 'wpcf-featured',
            'value' => true, // perhaps "true" instead?
            'compare' => '=' // or "LIKE"
        ),
);

$cars_query = new WP_Query( $args );

if ( $cars_query->have_posts() ) : while ( $cars_query->have_posts() ) : $cars_query->the_post(); ?>

<article></article>

<?php endwhile; ?>
<?php else: ?>
<?php endif; wp_reset_query(); ?>

Leave a Comment