0

I create one template of archive and taxonomy for all my CPT posts. Based on this I create search.php file and it's look like this:

<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>

In functions.php I add pre_get_posts:

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');

and example of my CPT in functions.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 );
}

but when I add $query->set with 'post_type' I can't recive any results with CPT posts... So, what can I do more?

  • 1
    When you register the post types you should just need to set exclude_from_search to false and this will be taken care of for you. – Jacob Peattie Mar 18 '18 at 15:15
  • Also, you don't need any of those arguments except for prev_text and next_text for paginate_links() if you're using the main query. – Jacob Peattie Mar 18 '18 at 15:17
  • I set 'exclude_from_search' to 'false', yes. But still the same. In my post above I've add my CPT from function, can you look? – Damian Mar 18 '18 at 15:28
  • @Damian could you show the code that registers your CPT? – Krzysiek Dróżdż Mar 18 '18 at 15:30
  • @Damian you shouldn’t need to change the query, if CPT is registered with ‘exclude_from_search’ set to false. Such CPT should be included in search results by default. Maybe there’s something else wrong with your query... (no “bonsai” matches the query, or something like this...)? – Krzysiek Dróżdż Mar 18 '18 at 15:36
0

I found a partial solution. When I add this part of code into my search form:

<input type="hidden" name="post_type[]" value="my_CPT_name" />

search results contains my CPT (even when 'exclude_from_search' is set to 'true').

Now, I change my pagination code to this:

<?php
echo paginate_links( array(
    'total' => $wp_query->max_num_pages,
    'prev_text'          => __(' « '),
    'next_text'          => __(' » '),
) );
    ?>

(@Jacob Peattie you mention that)

and everything works! But this hidden input makes URL ugly because when I search something, in search URL adress I see names of my every CPT... I still count for other advice so I do not mark my answer as a solution :)

  • Arę you sire that your pre_get_posts is firing up? Are there any other pre_get_posts? – Krzysiek Dróżdż Mar 18 '18 at 20:04
  • I have 2 pre_get_posts in function.php but first one refers only to archive pages. Second one is refers to search filters. – Damian Mar 18 '18 at 20:42
  • Could you remove that hidden field and var_dump wp_query? What SQL query does it generate? – Krzysiek Dróżdż Mar 18 '18 at 20:44
  • Ok. I don't know am I doing it right, but when I add <?php global $wp_query; var_dump($wp_query->query_vars); ?> before main loop I see this: dropbox.com/s/n18rlhh62z1wl03/… – Damian Mar 18 '18 at 20:54
  • OK, so it searches both CPTs, as you can see in there. The error must be somewhere else. – Krzysiek Dróżdż Mar 20 '18 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.