Tweaking Wordpress Search - Weighting Post Types to Rank Prominently

animalstyle

BuSo Pro
Joined
Mar 28, 2015
Messages
930
Likes
842
Degree
3
As I do when looking for solutions for Wordpress, I always search for non-plugin solutions, but I couldn't find a solution for this.

I am looking to weight a custom post type so that when you do a search, this custom post type always appears first. For instance a site about bowling alleys, when you search for a bowling alley by name like: "Joe's Alley" that the search results appear like this where the first result and the article results are different post types.

1. Joe's Bowling Alley
2. Joe's Bowling Alley Article 1
3. Joe's Bowling Alley Article 2

Right now, they rank by date which sometimes sees the obvious ranking appearing down the results a bit.

Anyone have any non-plugin recommendations to get this done?
 
Totally winging this because I'm actually on a break, but hopefully this can guide you down the right path.

I know it's possible to send in queries like this:
Code:
hxxp://www.domain.com/?s=search-term&post_type=company
Where you can attach a second parameter that only pulls results from the post type "company," for instance.

So that means there's a way to feed that data into the WP_Query somehow.

It'd be preferable to do this in the templates or as a function or plugin instead of in wp-includes, so it's not overwritten with upgrades....

Actually, why not edit your search.php template to include two loops. One that first only displays results from that custom post type, and then a 2nd loop that fills the rest of the options from the rest of your content, this time excluding the custom post type.

Something like this...

Code:
<!-- Skips all normal posts -->
<?php while (have_posts()) : the_post(); ?>
<?php if (is_search() && ($post->post_type!='company')) continue; ?>

.........

<!-- Run Loop again and exclude custom post type 'company' -->
<?php while (have_posts()) : the_post(); ?>
<?php if (is_search() && ($post->post_type=='company')) continue; ?>

That should put you on the path anyways. I'm not great with PHP still, and didn't test a lick of this.

You'd have to create some trickery with the first loop to maybe only show 1 result so it doesn't pull a ton of about Joe's Bowling Alley (and include John's Bowling Ball Shop), and if there's no result at all, don't show any message, just leave it blank and move on to the second loop.

Good luck with this, hope this helped some.
 
Thank you very much, looks like both of these should do the trick. I am going to set some time aside to test both of these out and see which way I like best.
 
Please let us know if you create a solution. That'd be great to have stored here for the future when one of us inevitably encounters the same scenario.
 
OK @Ryuzaki, below is my solution using your idea.

You can do some cool sorting with the WP Query within the loop. I used the multiple loops and some sorting to run two queries based on the search string. The custom post type for locations that I want to sort had unique taxonomies. Using this I did two loops, one that displays results in those taxonomies, and one that displays results NOT in those taxonomies: https://codex.wordpress.org/Class_Reference/WP_Query

Here is the example code:
Code:
<?php
//Select Locations
$atargs = array(
's' => get_search_query(),
'tax_query' => array(
    array(
        'taxonomy' => 'taxname',
        'field'    => 'slug',
        'terms'    => array( 'taxslug1', 'taxslug2' ),
    ),
),
);
$at_query = new WP_Query( $atargs ); ?>

<?php if ( $at_query->have_posts() ) : ?>

<h3>Locations:</h3>
<?php while ( $at_query->have_posts() ) : $at_query->the_post(); ?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php the_excerpt(); ?>
<?php endwhile; rewind_posts(); ?>

<?php else : ?>
<p><?php _e( 'Sorry, no locations found.' ); ?></p>
<?php endif; ?>

<?php
//Select Not Locations
$natargs = array(
's' => get_search_query(),
'tax_query' => array(
    array(
        'taxonomy' => 'taxname',
        'field'    => 'slug',
        'terms'    => array( 'taxslug1', 'taxslug2' ),
        'operator'  => 'NOT IN'
    ),
),
);
$nat_query = new WP_Query( $natargs ); ?>

<?php if ( $nat_query->have_posts() ) : ?>

<h3>Other Results:</h3>
<?php while ( $nat_query->have_posts() ) : $nat_query->the_post(); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php the_excerpt(); ?>
<?php endwhile; ?>

<?php else : ?>
<p><?php _e( 'Sorry, no other results.' ); ?></p>
<?php endif; ?>
 
Back