• Resolved wassy83

    (@wassy83)


    Hi to all I really cannot figure out this after so many hour of searching.. I’m developing this website(still 60%) with the divi theme. I created 3 custom posts type

    struttura
    tour
    offerta

    but I cannot obtain search results from these 3 cpt.
    Following this link
    https://docs.pluginize.com/article/23-post-type-posts-in-search-results
    I added the below code

    /*cpt ui search*/
    function my_cptui_add_post_type_to_search( $query ) {
    if ( is_admin() ) {
    return;
    }

    if ( $query->is_search() ) {
    $cptui_post_types = cptui_get_post_type_slugs();
    $query->set(
    ‘post_type’,
    array_merge(
    array( ‘post’, ‘page’ , ‘tour’ , ‘struttura’, ‘offerta’ ‘project’ ), // May also want to add the “page” post type.
    $cptui_post_types
    )
    );
    }
    }
    add_filter( ‘pre_get_posts’, ‘my_cptui_add_post_type_to_search’ );

    but the only effect of this code is that the project cpt are displayed(these project cpt are a cpt integrated in the divi theme)but the cpt created with cptui are still not showed in the search results. I have already tried with other search forms and even with the wp base search with same results..
    in example I cannot search this cpt
    https://neos.anekitalia.com/tours/tour-sulle-ombre-di-alessandro-magno/

    many thanks

    • This topic was modified 6 years, 3 months ago by wassy83.

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 20 total)
  • Thread Starter wassy83

    (@wassy83)

    I’m pasting here the code generated for the tour cpt

    function cptui_register_my_cpts_tour() {

    /**
    * Post Type: Tours.
    */

    $labels = array(
    “name” => __( “Tours”, “custom-post-type-ui” ),
    “singular_name” => __( “Tour”, “custom-post-type-ui” ),
    “all_items” => __( “Tutti i Tours”, “custom-post-type-ui” ),
    “add_new” => __( “Aggiungi tour”, “custom-post-type-ui” ),
    “search_items” => __( “cerca tour”, “custom-post-type-ui” ),
    );

    $args = array(
    “label” => __( “Tours”, “custom-post-type-ui” ),
    “labels” => $labels,
    “description” => “”,
    “public” => true,
    “publicly_queryable” => true,
    “show_ui” => true,
    “delete_with_user” => false,
    “show_in_rest” => true,
    “rest_base” => “”,
    “rest_controller_class” => “WP_REST_Posts_Controller”,
    “has_archive” => false,
    “show_in_menu” => true,
    “show_in_nav_menus” => true,
    “exclude_from_search” => false,
    “capability_type” => “post”,
    “map_meta_cap” => true,
    “hierarchical” => false,
    “rewrite” => array( “slug” => “tours”, “with_front” => false ),
    “query_var” => true,
    “menu_position” => 7,
    “menu_icon” => “/wp-content/uploads/media/icona-tour.png”,
    “supports” => array( “title”, “editor”, “thumbnail”, “excerpt” ),
    “taxonomies” => array( “tour_category” ),
    );

    register_post_type( “tour”, $args );
    }

    add_action( ‘init’, ‘cptui_register_my_cpts_tour’ );

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Couple things.

    1. without knowing exactly what the request query is, I’m not able to help too too much.
    2. Everything I can see, outside of maybe doing a bit too much with the pre_get_posts() hook, is what needs to be done typically.

    Try with this code snippet:

    function my_cptui_add_post_type_to_search( $query ) {
    	if ( is_admin() || ! $query->is_main_query() ) {
    		return;
    	}
    
    	if ( $query->is_search() ) {
    		$query->set(
    			'post_type',
    			array( 'post', 'page', 'tour', 'struttura', 'offerta' 'project' ),
    		);
    	}
    }
    add_filter( 'pre_get_posts', 'my_cptui_add_post_type_to_search' );
    

    1. Make sure it’s being added to your active theme’s functions.php.
    2. I amended what you used above to remove one function call, and removed the array merge. I also made it return early for the non-main query uses of WP_Query. This may or may not be playing a part.

    Thread Starter wassy83

    (@wassy83)

    thank you for your reply,
    I placed your code but unfortunately with same results, I can search blog posts,projects,pages but not the cpt created with cptui. I think that there is something wrong in the way I have created the taxonomies for these cpt.. but I cannot figure out. Is there something that I can check to help you giving some advice?
    many thanks

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Possible that there were some issues with the code above, specifically around some commas.

    This should be accurate and correct, syntax-wise:

    function my_cptui_add_post_type_to_search( $query ) {
    	if ( is_admin() || ! $query->is_main_query() ) {
    		return;
    	}
    
    	if ( $query->is_search() ) {
    		$query->set(
    			'post_type',
    			array( 'post', 'page', 'tour', 'struttura', 'offerta', 'project' )
    		);
    	}
    }
    
    add_filter( 'pre_get_posts', 'my_cptui_add_post_type_to_search' );
    

    You mention something in your last comment that has me curious. Are you trying to search by this tour_category taxonomy? Say one of the terms in that taxonomy is “studio”. Are you trying to search “studio” and hope that results show up, but none are? Out of box and by default, WordPress only searches post titles and post content. It doesn’t do any meta searching or taxonomy searching. So if you’re testing with values that aren’t found in the title or content, then it’s not going to find results for you.

    Thread Starter wassy83

    (@wassy83)

    thanks again for your support, I had already noticed the commas and syntax errors and I fixed them before posting the result, so nothing is changed. About the taxonomy.. I created a cpt called tour and the related taxonomy tour_category, then I have created some new posts inside tour, let’s say villa borghese and philippos(these are the posts title), then I tried to search through the divi search module, the wordpress search module, and an external plugin called ajax search the words villa, borghese, philippos but nothing is showed except from default wp posts, pages and divi cpt project(as I said the project wasn’t showed before your code).
    I’m searching for a fix from 2 days with no result..

    • This reply was modified 6 years, 3 months ago by wassy83.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    At this point, my only guess is perhaps $query->is_search() isn’t returning true somehow and thus the modifications aren’t actually getting applied at all.

    See what this version does:

    function my_cptui_add_post_type_to_search( $query ) {
    	if ( is_admin() || ! $query->is_main_query() ) {
    		return;
    	}
    
    	$query->set(
    		'post_type',
    		array( 'post', 'page', 'tour', 'struttura', 'offerta', 'project' )
    	);
    }
    
    add_filter( 'pre_get_posts', 'my_cptui_add_post_type_to_search' );
    

    It’s going to set all those post types to be included as part of everything, but if they start showing up in the search results, then we have a lead that you can go off of.

    Thread Starter wassy83

    (@wassy83)

    thank you for your patience I really appreciate it and sorry for the late answer but I’m writing from Italy.. Unfortunately your code does the same as before, I can search all posts including the cpt project of the divi theme but not the one created with cptui. Sorry for that but at this point I’m thinking that is something wrong in the way that I have created the cpt, maybe there is a settings that I didin’t have applied.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    No worries on the time differences. We both reply when we can.

    Your settings look fine to me, nothing looks off. I even created the post type in my local install and had results like expected. Only difference is I don’t have that taxonomy created, but that shouldn’t make a difference.

    I’m wondering if you have something else from elsewhere in your website that is somehow interfering, and this just hasn’t been found yet.

    Another idea/option to try would be to register the post type manually, outside of CPTUI and see if your results are any different or if they stay the same. If they stay the same, then it’s probably that “something not found” issue rather than CPTUI itself.

    Just trying to help.

    Thread Starter wassy83

    (@wassy83)

    ok I tried this simple code just to find a starting point and this is not giving me results anyway.. hmm.. I really don’t know why this is happening I’m thinking something at the server side(the site is hosted on my local High performance cluster with php7 and mariadb 10.1)

    // Our custom post type function
    function create_posttype() {
     
        register_post_type( 'movies',
        // CPT Options
            array(
                'labels' => array(
                    'name' => __( 'Movies' ),
                    'singular_name' => __( 'Movie' )
                ),
                'public' => true,
                'has_archive' => true,
    			'exclude_from_search' => false,
    			'publicly_queryable'  => true,
                'rewrite' => array('slug' => 'movies'),
            )
        );
    }
    // Hooking up our function to theme setup
    add_action( 'init', 'create_posttype' );
    Thread Starter wassy83

    (@wassy83)

    another strange thing is that in the backend results are ok

    Thread Starter wassy83

    (@wassy83)

    ok a little progress.. inspecting the generated url of the search results
    if I search in example philippos it will be like this

    https://neos.anekitalia.com/?s=philippos
    with no results but if I give

    https://neos.anekitalia.com/?s=philippos&post_status=all&post_type=struttura&action=-1&m=0&struttura_category&paged=1&action2=-1

    it will give me the right results. I got this url from the wp backend
    https://neos.anekitalia.com/wp-admin/edit.php?s=philippos&post_status=all&post_type=struttura&action=-1&m=0&struttura_category&paged=1&action2=-1

    Thread Starter wassy83

    (@wassy83)

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Definitely one way that is obviously producing some results, but very hands on and 99.9% of your visitors aren’t going to know to need to do that. 100% not ideal.

    Would you be willing to take this issue off the forums and into private communication? If yes, can you email support@pluginize.com and provide a link to this forum thread so we can keep things organized and know what relates to what.

    Thread Starter wassy83

    (@wassy83)

    ok thank you I just send an email with the details,
    for the moment a fix is this jquery in my header

    <script>
    jQuery(document).ready(function($){
    $(‘<input>’).attr({type: ‘hidden’,name: ‘post_type’,value : ‘struttura’ }).appendTo(‘#searchform’);
    $(‘<input>’).attr({type: ‘hidden’,name: ‘post_type’,value : ‘struttura’ }).appendTo(‘.widget_search form’);
    $(‘<input>’).attr({type: ‘hidden’,name: ‘post_type’,value : ‘struttura’ }).appendTo(‘.et_search_form_container form’);
    });
    </script>

    this will append the missing attributes to the search url of the 3 different search form that I have in my site(the divi one, the mobile one and the wp one), but is very unconvenient..

    many thanks again

    Hello, I’m having the same issue, any ideas on what was the solution? My search does not return any of CPT UI posts. I have also tried the code you created without luck.
    Thank you.

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘cannot search custom post type’ is closed to new replies.