1

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

  • Welcome to WPSE Dan. No guarantees on protecting your sanity I'm afraid. A few things to point out... query_posts: don't use it here. Note the note right at the top: codex.wordpress.org/Function_Reference/query_posts. From initial look at your code, you want the Featured businesses in your first loop only? There is an empty <article> tag so either you're showing us a partial of your code or you've neglected to output any info from the featured business here. – jdm2112 Oct 22 '15 at 15:28
  • Thanks for getting back to me @jdm2112.To save space in this question I omitted the code of the outputted views inside the article tags is what displays the results. Title, permalink etc. I just want to run an if else statement I think...if there is a featured business for this search query display it then display the rest else just display the regular listings. – Daniel Winnard Oct 22 '15 at 16:16
  • I think you may be looking for an answer I just posted yesterday (or a modified version thereof): wordpress.stackexchange.com/a/206166/21376 – s_ha_dum Oct 22 '15 at 16:20
  • I will take a look at this in full when I am at my desk. Thanks for posting. I think though my requirement is a little simpler as all I want to do is display the current results featured listings followed by the regular listings. I have it displaying as is but it only displays the featured listings in alphabetical order and then when you go to the next page it displays the next lot of featured listings. If you go to myweddingdreams.co.uk/wedding-directory and do a search for bridalwear in Kent you will see how the results are being displayed incorrectly. – Daniel Winnard Oct 22 '15 at 16:50
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(); ?>
  • Hi thanks for the code above, I can't seem to get it to work. I have currently got both parts of my script displaying. The featured businesses display and so do the regular listings, the problem is the featured businesses don't display first they are all over the place nestled inbetween regular listings. – Daniel Winnard Oct 23 '15 at 14:03
  • You'll need to specify the 2nd query to display all post EXCEPT the featured posts, so the 2nd query will look similar to the first one, except that the meta value parameter for key "wpcf-featured" is set to "false" rather than "true". – Taruc Oct 23 '15 at 14:15
  • Ok will give that a go and let you know how I get on. – Daniel Winnard Oct 23 '15 at 14:24
  • Tried that code and it didn't work. It displayed ALL the businesses set to FEATURED not the ones on the current pages search. – Daniel Winnard Oct 23 '15 at 14:41
  • If you go to link and do a search for Bridalwear -> Kent, you can see how it is displaying the results. – Daniel Winnard Oct 23 '15 at 14:43

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.