admin管理员组文章数量:1130349
I have a function that uses wp_get_recent_posts() I need to use this same function on my search page but am having problems adding the search parameter to the $args array.
Does anyone know if this is possible and if so how to implement it?
here is my function
function recent_articles_grid( $atts ) {
extract( shortcode_atts( array (
'numberposts' => 6,
'offset' => 0,
'featured' => null,
'trending' => null,
'showdate' => null,
'category' => null,
'showauthor' => null,
'init' => 1,
'searchterm' => null
), $atts ) );
$args = array(
'numberposts' => $numberposts,
'offset' => $offset,
'category__not_in' => array(391),
'category' => $category,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
... additional code ...
}
I have a function that uses wp_get_recent_posts() I need to use this same function on my search page but am having problems adding the search parameter to the $args array.
Does anyone know if this is possible and if so how to implement it?
here is my function
function recent_articles_grid( $atts ) {
extract( shortcode_atts( array (
'numberposts' => 6,
'offset' => 0,
'featured' => null,
'trending' => null,
'showdate' => null,
'category' => null,
'showauthor' => null,
'init' => 1,
'searchterm' => null
), $atts ) );
$args = array(
'numberposts' => $numberposts,
'offset' => $offset,
'category__not_in' => array(391),
'category' => $category,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
... additional code ...
}
Share
Improve this question
asked Oct 16, 2018 at 15:07
JasonJason
2052 silver badges12 bronze badges
8
|
Show 3 more comments
1 Answer
Reset to default 2It should just be a matter of setting the 's' argument of wp_get_recent_posts() (or just get_posts()) to the search term:
$args = array(
'numberposts' => $numberposts,
'offset' => $offset,
'category__not_in' => array(391),
'category' => $category,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
's' => $searchterm,
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
But yeah, as discussed in the comments, I wouldn't suggest this method of display search results. If you're using search.php correctly, the main query/loop will already contain search results.
A better question might be how to get posts from the main query into your layout function. Otherwise you're just needlessly performing the search twice, and you're going to be running into issues with pagination, because you won't be using the loop and template hierarchy properly.
I have a function that uses wp_get_recent_posts() I need to use this same function on my search page but am having problems adding the search parameter to the $args array.
Does anyone know if this is possible and if so how to implement it?
here is my function
function recent_articles_grid( $atts ) {
extract( shortcode_atts( array (
'numberposts' => 6,
'offset' => 0,
'featured' => null,
'trending' => null,
'showdate' => null,
'category' => null,
'showauthor' => null,
'init' => 1,
'searchterm' => null
), $atts ) );
$args = array(
'numberposts' => $numberposts,
'offset' => $offset,
'category__not_in' => array(391),
'category' => $category,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
... additional code ...
}
I have a function that uses wp_get_recent_posts() I need to use this same function on my search page but am having problems adding the search parameter to the $args array.
Does anyone know if this is possible and if so how to implement it?
here is my function
function recent_articles_grid( $atts ) {
extract( shortcode_atts( array (
'numberposts' => 6,
'offset' => 0,
'featured' => null,
'trending' => null,
'showdate' => null,
'category' => null,
'showauthor' => null,
'init' => 1,
'searchterm' => null
), $atts ) );
$args = array(
'numberposts' => $numberposts,
'offset' => $offset,
'category__not_in' => array(391),
'category' => $category,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
... additional code ...
}
Share
Improve this question
asked Oct 16, 2018 at 15:07
JasonJason
2052 silver badges12 bronze badges
8
- Why do you need to use this on your search page? If you're using search.php correctly, the main query/loop will already contain search results. – Jacob Peattie Commented Oct 16, 2018 at 15:20
- In the "additional code" section of the function I am building out a complex grid system and I would like to not have to rebuild this on the search template but instead utilize the function I have already built. – Jason Commented Oct 16, 2018 at 15:22
- Your question should then be how to get posts from the main query into your layout function. Otherwise you're just needlessly performing the search twice, and you're going to be the next person running into issues with pagination, because you're not using the loop and template hierarchy properly. – Jacob Peattie Commented Oct 16, 2018 at 15:27
- ok, understood. so your answer then is this is NOT recommended which is the path I was heading down, however, I am still curious if it is even possible. – Jason Commented Oct 16, 2018 at 15:29
-
It should just be a matter of setting the
's'argument ofwp_get_recent_posts()(or justget_posts()) to the search term, but yeah, I wouldn't suggest this method of display search results. – Jacob Peattie Commented Oct 16, 2018 at 15:32
1 Answer
Reset to default 2It should just be a matter of setting the 's' argument of wp_get_recent_posts() (or just get_posts()) to the search term:
$args = array(
'numberposts' => $numberposts,
'offset' => $offset,
'category__not_in' => array(391),
'category' => $category,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
's' => $searchterm,
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
But yeah, as discussed in the comments, I wouldn't suggest this method of display search results. If you're using search.php correctly, the main query/loop will already contain search results.
A better question might be how to get posts from the main query into your layout function. Otherwise you're just needlessly performing the search twice, and you're going to be running into issues with pagination, because you won't be using the loop and template hierarchy properly.
本文标签: phpUse wpgetrecentposts with search term
版权声明:本文标题:php - Use wp_get_recent_posts with search term 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749257807a2340778.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


's'argument ofwp_get_recent_posts()(or justget_posts()) to the search term, but yeah, I wouldn't suggest this method of display search results. – Jacob Peattie Commented Oct 16, 2018 at 15:32