在函数中放置任何过滤器。php
Using pre get posts
// Filter the search page
add_filter(\'pre_get_posts\', \'search_pre_get_posts\');
function search_pre_get_posts($query)
{
// Verify that we are on the search page that that this came from the event search form
if($query->query_vars[\'s\'] != \'\' && is_search())
{
// If "s" is a positive integer, assume post id search and change the search variables
if(absint($query->query_vars[\'s\']))
{
// Set the post id value
$query->set(\'p\', $query->query_vars[\'s\']);
// Reset the search value
//$query->set(\'s\', \'\');
}
}
}
Using posts where query
add_filter(\'posts_where\', \'posts_where\');
function posts_where($where)
{
$s = $_GET[\'s\'];
if(!empty($s))
{
if(is_numeric($s))
{
global $wpdb;
$where = str_replace(\'(\' . $wpdb->posts . \'.post_title LIKE\', \'(\' . $wpdb->posts . \'.ID = \' . $s . \') OR (\' . $wpdb->posts . \'.post_title LIKE\', $where);
}
elseif(preg_match("/^(\\d+)(,\\s*\\d+)*\\$/", $s)) // string of post IDs
{
global $wpdb;
$where = str_replace(\'(\' . $wpdb->posts . \'.post_title LIKE\', \'(\' . $wpdb->posts . \'.ID in (\' . $s . \')) OR (\' . $wpdb->posts . \'.post_title LIKE\', $where);
}
}
return $where;
}
根据需要使用任何一种功能。第一个函数使用搜索id进行精确搜索。例如:1只提供1个id的帖子。
现在如何显示帖子的所有价值,我给你提示。
编辑您的搜索。php或添加搜索。主题中的php。基本上每个主题都有搜索。php,所以您只需要稍微修改一下。
我给你一些搜索的示例代码。php。您将根据需要对其进行修改。
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$post_id = get_the_ID();
$id = get_post_thumbnail_id($post_id);
$image = wp_get_attachment_image_url($id,\'full\');
?>
// Print The title
<?php echo get_the_title($post_id); ?>
//print full content of the post
<?php echo get_the_content(); ?>
//print the excert
<?php print get_the_excerpt($post_id); ?>
// print the image
<img src="<?php echo $image; ?>" class="attachment-portfolio-three size-portfolio-three wp-post-image" alt="">
// suppose custom field
<a href="<?php echo get_the_permalink($post_id); ?>">
<?php echo get_post_meta($post_id,\'custom_field_1\',true);?>
</a>
// suppose custom field 2
<a href="<?php echo get_the_permalink($post_id); ?>">
<?php echo get_post_meta($post_id,\'custom_field_2\',true);?>
</a>
// suppose custom field 3
<a href="<?php echo get_the_permalink($post_id); ?>">
<?php echo get_post_meta($post_id,\'custom_field_2\',true);?>
</a>
<?php endwhile;?>
<?php wp_reset_query();
// print pagination and also its arg if you use my code.
paginate_links($args);
else :
?>
<p><?php _e( \'Sorry, but nothing matched your search terms. Please try again with some different keywords.\', \'twentyseventeen\' ); ?></p>
<?php
get_search_form();
endif;
?>