使用wordpress标准搜索表单搜索帖子和页面 - 挂钩无法正常工作

时间:2016-11-21 08:48:36

标签: php wordpress search

我使用这个动作钩子:

function search_filter($query) {
    if ( !is_admin() && $query->is_main_query() ) {
        if ($query->is_search) {
            $query->set('post_type', array( 'post', 'snackcontent', 'page' ) );
        }
    }
}

add_action('pre_get_posts','search_filter');

这是来自wordpress文档:

https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

但我仍然只有帖子。

还有其他技巧吗?

编辑:

这真的是一个action_hook吗?

我看到一些教程他们使用相同的pre_get_posts但是在filter_hook中。

感谢您的评论和解答。

我认为这是我输出结果的问题。

我查看了我的搜索的SQL查询:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND (((wp_posts.post_title LIKE '%impressum%') OR (wp_posts.post_excerpt LIKE '%impressum%') OR (wp_posts.post_content LIKE '%impressum%'))) AND wp_posts.post_type IN ('post', 'page', 'snackcontent') AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') ORDER BY wp_posts.post_title LIKE '%impressum%' DESC, wp_posts.post_date DESC LIMIT 0, 10

通过此查询,我得到了丢失的页面。

现在我在search.php中搜索问题:

<section id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

    <?php if ( have_posts() ) : ?>

        <header class="page-header">
            <h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'twentyfifteen' ), get_search_query() ); ?></h1>
        </header><!-- .page-header -->

        <?php
        // Start the loop.
        while ( have_posts() ) : the_post();
            /*
             * Run the loop for the search to output the results.
             * If you want to overload this in a child theme then include a file
             * called content-search.php and that will be used instead.
             */

            if(get_the_post_thumbnail()){ ?>
                <?php
                $select = new Dynamic_Startsite_Widget();
                $select_status = $select->getPrimaryCategory(get_the_ID());

                ?>
                <div id="post-<?php the_ID(); ?>" class="col-xs-12 col-sm-4 dmd-dyn-border outerHolder small_box_three search-wrapper">
                    <div><a href="<?php echo get_permalink(); ?>"></a>
                        <div class="siteorigin-widget-tinymce textwidget"><a href="<?php echo get_permalink(); ?>"></a>
                            <div class="dmd-box-transparent"><a href="<?php echo get_permalink(); ?>">
                                    <? echo get_the_post_thumbnail(); ?>
                                    <div class="row search-content">
                                        <div class="col-md-12 lh35">
                                            <div class=""><p class="col-md-9 start-page-post-category"><?php echo $select_status ?></p></div>
                                            <div class="col-md-3 col-sm-4 start-page-post-favorite-images text-right">
                                                <div>
                                                    <p class="<?php $id[] = the_ID(); ?> icon-favorite <?php echo dmd_search_check_select($id); ?>" wp-post-id="<?php the_ID(); ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Klicken, wenn Ihnen dieser Artikel gefällt"></p>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col-md-12 col-sm-8 search-headline">
                                            <h2>
                                                <a class="search-page-post-title" href="<?php echo get_permalink(); ?>"><? echo get_the_title(); ?></a>
                                            </h2>
                                        </div>
                                    </div>
                            </div>
                        </div>
                    </div>
                </div>

            <?php }

        // End the loop.
        endwhile;

    else :
        get_template_part( 'content', 'none' );
    endif;
    ?>

    </main><!-- .site-main -->
</section><!-- .content-area -->

0 个答案:

没有答案