0

I'm trying to manually build a query in 'loop.php' that outputs a list of 3 posts from a custom post type above the results automatically given when doing a search in WordPress.

As such, I've got the following code that displays these results as I intended:

<?php   
// The Query
$query1 = new WP_Query( array( 'post_type' => 'sdm_downloads', 'posts_per_page' => 3) );

if ( $query1->have_posts() ) :
    echo '<div style="background-color:#fffced; padding:10px;margin-bottom:20px;">';
    echo '<h3>Selected relevant items from our Briefings library:</h3>';
    // The Loop
    while ( $query1->have_posts() ) {
        $query1->the_post();
    <article class="article-archive" id="post-<?php the_ID(); ?>">

        <header>
            <hgroup>
                <p><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
                <p>
                <!--<p class="entry-meta">Posted <strong><?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago'; ?></strong></p>-->
            </hgroup>
        </header>
    </article>
<?php
}
echo '</div>';
endif
?>

This is great, except that it just pulls out the three most recent posts from this post type. So, I figured I'd need to manually add in the search terms as follows:

$query1 = new WP_Query( array( 'post_type' => 'sdm_downloads', 'posts_per_page' => 3, 's' => $s) );

However this shows nothing at all. Likewise if I replace the s variable with a string, I still get no posts returned. I've even removed all arguments except for the string and still get no results eg:

$query1 = new WP_Query( array( 's' => 'awards') );

Any ideas, as to why this is happening &/or how it can be fixed?

Thanks.


If I do a dump of the query without specifying the 's' argument I get the following

string(226) "SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'sdm_downloads' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY wp_posts.post_date DESC LIMIT 0, 3"

Whereas if I add an 's' argument to the WP_Query, I get the following dump:

string(32) "SELECT * FROM wp_posts WHERE 1=2" 

It appears that this behaviour only occurs when I have the Relevanssi search plugin enabled. I guess I'll go ask over at the makers website, to see if they can shed any light on why this is happening.

In the meantime if anyone has any suggestions on how to deal with this, I'd be happy to hear them.

6
  • I don't see a problem (and my tests work). Run var_dump($query1->request) and edit the result into the question.
    – s_ha_dum
    Oct 19, 2015 at 14:20
  • @s_ha_dum Ive updated my OP with some dumps. Oct 19, 2015 at 18:32
  • Use only the s argument. What do you get? Are you running any plugins that might be altering the query?
    – s_ha_dum
    Oct 19, 2015 at 18:35
  • Using only the 's' argument also returns: string(32) "SELECT * FROM wp_posts WHERE 1=2" . This only seems to happen when I've got the 'Relevanssi' search plugin activated. So it appears to be due to that. As mentioned in my edit, I'll see if the plugin makers can offer any advice. Thanks for the help. Oct 19, 2015 at 18:38
  • There is a filter on the query that is breaking your search. You are going to need to track that down. Disable plugins, first off. Then dig through your theme for query related filters/actions.
    – s_ha_dum
    Oct 19, 2015 at 18:40

1 Answer 1

0

If you are using Relevanssi, it is messing with the native search (don't know exactly at which point), thus, the standard query doesn't work.

I have tested a solution, according to what I read in the plugin's page, and it seems to do the trick. Just run the relevanssi function to append the results to the query, in this way:

relevanssi_do_query($query1);

Note that the parameter is the query object you created before:

$query1 = new WP_Query( array( 'post_type' => 'sdm_downloads', 'posts_per_page' => 3) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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