Created
November 12, 2021 16:27
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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