admin管理员组文章数量:1122832
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.
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.
Share Improve this question edited Oct 20, 2015 at 9:23 Phill Healey asked Oct 19, 2015 at 14:07 Phill HealeyPhill Healey 3781 gold badge7 silver badges20 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 0If 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) );
本文标签: custom post typesManually build WPQuery
版权声明:本文标题:custom post types - Manually build WP_Query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736293441a1929146.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
var_dump($query1->request)
and edit the result into the question. – s_ha_dum Commented Oct 19, 2015 at 14:20s
argument. What do you get? Are you running any plugins that might be altering the query? – s_ha_dum Commented Oct 19, 2015 at 18:35