使用PRE_GET_POST将CPT添加到搜索结果

时间:2018-03-18 作者:Damian

我为我所有的CPT帖子创建了一个归档和分类模板。基于此,我创建了搜索。php文件,如下所示:

<div class="center">
                    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                    <?php include \'product.php\' ?>
                    <?php endwhile ?>
                    <nav class="pagination">
                        <?php
    $big = 999999999; // need an unlikely integer
echo paginate_links( array(
    \'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
    \'format\' => \'?paged=%#%\',
    \'current\' => max( 1, get_query_var(\'paged\') ),
    \'total\' => $wp_query->max_num_pages,
    \'prev_text\'          => __(\' « \'),
    \'next_text\'          => __(\' » \'),
) );
    ?>
                    </nav>
                    <?php else : ?>
                    <p>
                        <?php esc_html_e( \'Sorry, no posts matched your criteria.\' ); ?>
                    </p>
                    <?php endif; ?>
                </div>
在函数中。php I add pre\\u get\\u帖子:

function search_filter($query) {
  if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_search) {
      $query->set(\'post_type\', array( \'drzewa_formowane\', \'pre_bonsai\' ) );
    }
  }
}

add_action(\'pre_get_posts\',\'search_filter\');
以及我在函数中的CPT示例。php:

/**
register custom post type
**/
//http://codex.wordpress.org/Function_Reference/register_post_type
add_action( \'init\', \'drzewa_formowane\' );
function drzewa_formowane() {
    $labels = array(
        \'name\'               => _x( \'Drzewa formowane\', \'post type general name\', \'your-plugin-textdomain\' ),
        \'singular_name\'      => _x( \'drzewa formowane\', \'post type singular name\', \'your-plugin-textdomain\' ),
        \'menu_name\'          => _x( \'Drzewa\', \'admin menu\', \'your-plugin-textdomain\' ),
        \'name_admin_bar\'     => _x( \'Drzewa formowane\', \'add new on admin bar\', \'your-plugin-textdomain\' ),
        \'add_new\'            => _x( \'Dodaj nowe\', \'book\', \'your-plugin-textdomain\' ),
        \'add_new_item\'       => __( \'Dodaj nowe\', \'your-plugin-textdomain\' ),
        \'new_item\'           => __( \'Nowe\', \'your-plugin-textdomain\' ),
        \'edit_item\'          => __( \'Edytuj\', \'your-plugin-textdomain\' ),
        \'view_item\'          => __( \'Zobacz\', \'your-plugin-textdomain\' ),
        \'all_items\'          => __( \'Wszystkie\', \'your-plugin-textdomain\' ),
        \'search_items\'       => __( \'Szukaj\', \'your-plugin-textdomain\' ),
        \'parent_item_colon\'  => __( \'Nadrzędne drzewo:\', \'your-plugin-textdomain\' ),
        \'not_found\'          => __( \'Nie znaleziono.\', \'your-plugin-textdomain\' ),
        \'not_found_in_trash\' => __( \'Nie znaleziono nic w koszu.\', \'your-plugin-textdomain\' )
    );
    $args = array(
        \'labels\'             => $labels,
        \'description\'        => __( \'Drzewa formowane bonsai\', \'your-plugin-textdomain\' ),
        \'public\'             => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\' => true,
        \'show_ui\'            => true,
        \'show_in_menu\'       => true,
        \'query_var\'          => true,
        \'capability_type\'    => \'post\',
        \'has_archive\'        => true,
        \'hierarchical\'       => false,
        \'menu_icon\'          => \'dashicons-palmtree\', // dodaje ikonkę z Dashicons https://developer.wordpress.org/resource/dashicons
        \'menu_position\'      => 5,
        \'supports\'           => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' )
    );
    register_post_type( \'drzewa_formowane\', $args );
}
/**
register custom hierarchical taxonomy, like categories
**/
// https://codex.wordpress.org/Function_Reference/register_taxonomy
add_action( \'init\', \'drzewa_formowane_categories\', 0 );
function drzewa_formowane_categories() {
    $labels = array(
        \'name\'              => _x( \'Kategorie\', \'taxonomy general name\', \'textdomain\' ),
        \'singular_name\'     => _x( \'Kategorie\', \'taxonomy singular name\', \'textdomain\' ),
        \'search_items\'      => __( \'Przeszukaj kategorie\', \'textdomain\' ),
        \'all_items\'         => __( \'Wszystkie kategorie\', \'textdomain\' ),
        \'parent_item\'       => __( \'Nadrzędna kategoria\', \'textdomain\' ),
        \'parent_item_colon\' => __( \'Nadrzędna kategoria:\', \'textdomain\' ),
        \'edit_item\'         => __( \'Edytuj kategorię\', \'textdomain\' ),
        \'update_item\'       => __( \'Uaktualnij kategorię\', \'textdomain\' ),
        \'add_new_item\'      => __( \'Dodaj kategorię\', \'textdomain\' ),
        \'new_item_name\'     => __( \'Nowa nazwa kategorie\', \'textdomain\' ),
        \'menu_name\'         => __( \'Kategorie\', \'textdomain\' ),
    );
    $args = array(
        \'hierarchical\'      => true,
        \'labels\'            => $labels,
        \'show_ui\'           => true,
        \'show_admin_column\' => true,
        \'query_var\'         => true,
    );
    register_taxonomy( \'drzewa_formowane_categories\', array( \'drzewa_formowane\' ), $args );
}
但是,当我添加$query->设置为“post\\u type”时,我无法接收到CPT帖子的任何结果。。。那么,我还能做些什么呢?

1 个回复
SO网友:Damian

我找到了部分解决方案。当我将这部分代码添加到搜索表单中时:

<input type="hidden" name="post_type[]" value="my_CPT_name" />
搜索结果包含我的CPT(即使“exclude\\u from\\u search”设置为“true”)。

现在,我将分页代码更改为:

<?php
echo paginate_links( array(
    \'total\' => $wp_query->max_num_pages,
    \'prev_text\'          => __(\' « \'),
    \'next_text\'          => __(\' » \'),
) );
    ?>
(@Jacob Peattie你提到过)

一切都正常!但这个隐藏的输入让URL很难看,因为当我搜索某物时,在搜索URL地址中,我会看到我的每个CPT的名称。。。我仍然会考虑其他建议,因此我不会将我的答案标记为解决方案:)

结束

相关推荐

making a search.php query

我正在尝试创建一个自定义搜索结果页面。我希望一次有6篇帖子,然后我要添加一个加载更多的按钮,可以加载更多的6篇帖子,以此类推。我希望页面处于引导网格布局中。每行2个立柱(col-md-6)。使用下面的代码,网页甚至不会显示出来。我是否需要向我的函数添加任何内容。php来实现这一点?有人能帮我吗?提前谢谢。<?php get_header(); $the_query = new WP_Query( [ \'posts_per_page\' =>