Skip to content

Instantly share code, notes, and snippets.

@thierrypigot
Created November 12, 2021 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thierrypigot/2f65329d3ea58901dae07645ba296fcf to your computer and use it in GitHub Desktop.
Save thierrypigot/2f65329d3ea58901dae07645ba296fcf to your computer and use it in GitHub Desktop.
WordPress search post type by post type. (Beaver Builder template file in this exemple)
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Silence is golden.
}
?>
<?php get_header(); ?>
<div class="fl-archive <?php FLLayout::container_class(); ?>">
<div class="<?php FLLayout::row_class(); ?>">
<?php FLTheme::sidebar( 'left' ); ?>
<?php
$search_query = get_search_query();
$posts_type = get_post_types( array( 'public' => true, '_builtin' => false ) );
unset( $posts_type['fl-builder-template'] );
$posts_type = array_merge( array( 'post', 'page' ), $posts_type );
foreach( $posts_type as $post_type ){
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1,
'orderby' => 'name',
's' => $search_query
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$post_type_info = get_post_type_object( $post_type );
?>
<h2>Résultat(s) pour les éléments de type : <?php echo $post_type_info->labels->name; ?></h2>
<div class="fl-content <?php FLLayout::content_class(); ?>"<?php FLTheme::print_schema( ' itemscope="itemscope" itemtype="https://schema.org/Blog"' ); ?>>
<?php
while ( $the_query->have_posts() ) {
$the_query->the_post();
get_template_part( 'content' );
}
?>
</div>
<?php
}
wp_reset_postdata();
}
?>
<?php FLTheme::sidebar( 'right' ); ?>
</div>
</div>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment