0

I saw that similar questions have already been asked and I tested everything they answered.

the answer that partially solved my problem was:

global $wp_query;
$args = array_merge( $wp_query->query, array( 'post_type' => 'post' ) );
query_posts( $args );

but I'm using a theme that search needs to return type 'manga' and it doesn't. I tried with page and with post and it worked but with manga it didn't.

Someone knows how to fix it?

Here's all the code:

query, array( 'post_type' => 'manga' ) ); query_posts( $args ); ?>
<div class="c-search-header__wrapper" style="<?php echo esc_attr($search_header_background != '' ? $search_header_background : 'background-image: url(' . get_parent_theme_file_uri( '/images/bg-search.jpg' ) . ');'); ?>">
    <div class="container">
        <div class="search-content">
            <form role="search" method="get" class="search-form <?php echo (esc_html($madara_ajax_search) == 'on' ? 'ajax' : '');?>">
                <label> <span class="screen-reader-text"><?php esc_html_e( 'Search for:', 'madara' ); ?></span>
                    <input type="search" class="search-field" placeholder="<?php esc_html_e( 'Search...', 'madara' ); ?>" value="<?php echo esc_attr( $s ); ?>" name="s">
                </label> <input type="submit" class="search-submit" value="<?php esc_html_e( 'Search', 'madara' ); ?>">
            </form>
        </div>
    </div>
</div>
<div class="c-page-content">
    <div class="content-area">
        <div class="container">
            <div class="row">
                <div class="main-col col-md-12 sidebar-hidden">

                    <?php get_template_part( 'html/main-bodytop' ); ?>

                    <!-- container & no-sidebar-->
                    <div class="main-col-inner">
                        <?php
                            if ( have_posts() ) {
                        ?>
                        <div class="search-wrap">
                            <div class="tab-wrap">
                                <div class="c-blog__heading style-2 font-heading">
                                    <h4>
                                        <i class="<?php madara_default_heading_icon(); ?>"></i>
                                        <?php echo sprintf( _n( '%s result', '%s results', $wp_query->found_posts, 'madara' ), $wp_query->found_posts ); ?>
                                    </h4>
                                </div>
                            </div>
                            <!-- Tab panes -->
                            <div class="tab-content-wrap">
                                <div class="c-blog-listing">
                                    <div class="c-blog__inner">
                                        <div class="c-blog__content">
                                            <div id="loop-content" class="page-content-listing">
                                                <?php
                                                    /* Start the Loop */
                                                    $index = 1;
                                                    set_query_var( 'madara_post_count', madara_get_post_count() );

                                                    while ( have_posts() ) : the_post(); ?><?php

                                                        set_query_var( 'madara_loop_index', $index );

                                                        get_template_part( 'html/loop/content', get_post_format() );

                                                        $index ++;
                                                        ?>

                                                    <?php endwhile; ?>
                                            </div>
                                        </div>

                                        <?php

                                            //Get Pagination
                                            $madara_pagination = new App\Views\ParsePagination();
                                            $madara_pagination->renderPageNavigation( '#loop-content', 'html/loop/content' );

                                        ?>


                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php
                    } else {
                    ?>
                    <div class="search-wrap no-results not-found">
                        <div class="results_content">
                            <div class="icon-not-found">
                                <i class="icon ion-android-sad"></i>
                            </div>
                            <div class="not-found-content">
                                <p><?php esc_html_e( 'No matches found. Try a different search...', 'madara' ); ?></p>
                            </div>
                        </div>
                    </div>
                    <?php
                    }
                    
                    get_template_part( 'html/main-bodybottom' ); ?>

                </div>
            </div>
        </div>
    </div>
</div></div>
  • Try using the pre_get_posts hook rather than editing the query at the template level :) https://stackoverflow.com/questions/24195818/add-results-into-wordpress-search-results – admcfajn Jun 10 '22 at 18:32
  • I've already tried. Like i said bellow – Rapha Gosh Jun 10 '22 at 20:18
  • There must be something else preventing it from working then. It won't work if you don't have a loop in your template, for example. try adding `var_dump($query);exit` inside of the pre_get_posts function to help determine what's going wrong. It could be a lot of things depending on how your site is set-up. pre_get_posts filter is the way to do what you're asking. – admcfajn Jun 10 '22 at 20:54

1 Answers1

0

I suspect, assuming 'manga' is a custom post type, that you want to look at sommething like https://thomasgriffin.com/how-to-include-custom-post-types-in-wordpress-search-results/

Try

function a_include_custom_post_types_in_search_results( $query ) {
    if ( $query->is_main_query() && $query->is_search() && ! is_admin() ) {
        $query->set( 'post_type', array( 'post', 'manga') );
    }
}
add_action( 'pre_get_posts', 'a_include_custom_post_types_in_search_results' );

Chances are with it or something like it in functions.php you won't need to do much chanching of your theme's search teplates.

Andre Clements
  • 634
  • 1
  • 7
  • 15
  • Nothing yet :c Idk why but it didn't work – Rapha Gosh Jun 10 '22 at 18:46
  • and you are not using any plugins that may be overriding what happens with search results? – Andre Clements Jun 10 '22 at 18:48
  • By default when you create a manga it enters the manga category and also the main page is named manga where all the ones created before are located. It doesn't seem to behave like a post category but something separate. – Rapha Gosh Jun 10 '22 at 18:49
  • No :C I'm not using a search plugin cuz i really want the original to work. The appearance is different – Rapha Gosh Jun 10 '22 at 18:49
  • oh, so it is a 'post' or is post_type 'manga'. Too debug you can run `global $wpdb; // Print last SQL query string echo $wpdb->last_query;` after get_post to try and see what is being queried – Andre Clements Jun 10 '22 at 18:51
  • I debbuged inside the wordpess and it showed error (null string) $path = str_replace( '', '', $path );" can it be related? – Rapha Gosh Jun 10 '22 at 19:30
  • I saw it can be related to search. errors in functions.php (null values) – Rapha Gosh Jun 10 '22 at 19:34