Forums

[resolved] search for cutom post type in a custom taxonomy (2 posts)

  1. paka
    Member
    Posted 8 months ago #

    Hello,

    How can I search for a custom post in a custom tyxonomy, or how do I have to build the GET-varable.
    I've got a custom post type 'product' and a custom taxonomy 'product group'. Now I need a search form, with the posibility to search for a product in a choosen product group.
    To do so, I use this Code:

    <form id="searchform" action="<?php bloginfo('url'); ?>/" method="get">
    			<input id="s" maxlength="150" name="s" size="20" type="text" value="" class="txt" />
    			<input name="post_type" type="hidden" value="<?php echo $postType ?>" />
    			in <?php wp_dropdown_categories( array( 'taxonomy' => 'as_tax_product') 	); ?>
    			<input id="searchsubmit" class="btn" type="submit" value="Search" />
    			</form>

    If I search for a existing product with the right product group choosen, I get no result, but this messages:
    Notice: Trying to get property of non-object in C:\Program Files\xampp\xampp\htdocs\wp_shop\wp-includes\general-template.php on line 1652

    Notice: Trying to get property of non-object in C:\Program Files\xampp\xampp\htdocs\wp_shop\wp-includes\general-template.php on line 1653

    If I don't send the post_type, I get more Notices. this is something I don't understand, because I limit the search with:

    function SearchFilter($query) {
    				  if ($query->is_search) {
    				    $query->set('post_type', 'as_post_product');
    				  }
    				  return $query;
    	}
    add_filter('pre_get_posts',array($this, 'SearchFilter'));

    All of the Notices mention lines in the code, which have something to do with the category, so I suppose, I cannot query for my custom posts in this way.
    With the form above, the GET varable looks like:
    http.../?s=ball&post_type=as_post_product&cat=18.
    the SQL Query looks like:
    SELECT ..... WHERE 1=1 AND 0=1 AND...
    Everything in the query is OK only the 1=1 AND 0=1 I don't understand, and I think, the 0=1 has to do with the problem as well.

    I hope, I posted all info needed.
    Every tip would be nice.

    Greets Paka

  2. paka
    Member
    Posted 8 months ago #

    Solved,
    two days I was searching for a solution, not one hour after I post here, I've found it.
    The GET has to look like: ?s=ball&post_type=as_post_product&as_tax_product=Spielzeug. Spielzeug is the name of the product group.

    So I insert a select myself:

    $taxProductCats = get_categories(array('taxonomy' => $taxName )) ;
    myVarDump($taxProductCats , 'liste der Produkt gruppen') ;
    ?>
    	<form id="searchform" action="<?php bloginfo('url'); ?>/" method="get">
    	<input id="s" maxlength="150" name="s" size="20" type="text" value="" class="txt" />
    	<input name="post_type" type="hidden" value="<?php echo $postType ?>" />
    	<select name="<?php echo $taxName ?>">
    	<?php
    		foreach ($taxProductCats as $prodCatobjkt){
    			echo '<option value="'.$prodCatobjkt->name.'">'.$prodCatobjkt->name.'</option>' ;
    		}
    	?>
    	</select>
    	<input id="searchsubmit" class="btn" type="submit" value="Search" />
    	</form>

    No more messages, and all searched products are found.
    Wonderfull.

Reply

You must log in to post.

About this Topic