0

I've created a custom-search-page for my theme that have to search in my CPT non-hierarchical taxonomy and eventually filter the results if some preference is inserted through the options form of custom-fields.

i have and advanced-searchform.php that's just a form

    <input type="hidden" name="search" value="advanced">

<input type="text" value="" placeholder="<?php _e( 'Cosa c\'è in frigo?', 'whattodo' ); ?>" name="ing" id="ing" />

and three select for this kind of post custom fields.

<select name="tempo" id="tempo">
<select name="tempo" id="portata">
<select name="tempo" id="difficolta">

All of this run into advanced-search-results.php with the following code,plus the query loop at the bottom of the page who i didn't include now.

 <?php
// Get data from URL into variables
$_ing = $_GET['ing'] != '' ? $pieces = explode(" ", $_GET['ing']) : '';
$_tempo = $_GET['tempo'] != '' ? $_GET['tempo'] : '';
$_portata = $_GET['portata'] != '' ? $_GET['portata'] : '';
$_difficolta = $_GET['difficolta'] != '' ? $_GET['difficolta'] : '';



// VERIFY IF SOMETHING WAS TYPED IN SEARCH
if( ! empty( $pieces ) ){

//BUILDING ARRAY FOR TAX_QUERY RELATIONS
foreach( $pieces as $pezzo ){
                $tag[] = array(
                    'taxonomy' => 'ingredienti',
                    'terms' => "'".$pezzo."'",
                    'field' => 'slug');
        };
}

// Start the Query
$v_args = array(
        'post_type'     =>  'ricetta', // your CPT
        'tax_query'           => array(
                            'relation' => 'AND',
                             $tag,
            ),
'meta_query'    =>  array(
                                array(
                                    'key'     => 'tempo_di_realizzazione',                                         'value'   => $_tempo,
                                    'compare' => 'LIKE', 
                                ),
                                array(
                                    'key'     => 'portata', 
                                    'value'   => $_portata,
                                    'compare' => 'LIKE',                                     ),array(
                                    'key'     => 'difficolta',                                         'value'   => $_difficolta,
                                    'compare' => 'LIKE',                                     )

                            )
    );
$ricettaSearchQuery = new WP_Query( $v_args );

// Open this line to Debug what's query WP has just run
var_dump($ricettaSearchQuery->request);

here is the problem.If i run this kind of tax_query in the index.php page without the custom meta fields,all runs smooth and i obtain the kind of post that i expect to.But if i run this in this form,i achive just nothing found,it's like wordpress can't read the posts taxonomy from this page.

i attach the var_dump() of my query.

string(813) "SELECT SQL_CALC_FOUND_ROWS wtdx_posts.ID FROM wtdx_posts INNER JOIN wtdx_postmeta ON ( wtdx_posts.ID = wtdx_postmeta.post_id ) INNER JOIN wtdx_postmeta AS mt1 ON ( wtdx_posts.ID = mt1.post_id ) INNER JOIN wtdx_postmeta AS mt2 ON ( wtdx_posts.ID = mt2.post_id ) WHERE 1=1 AND ( ( 0 = 1 ) ) AND ( ( wtdx_postmeta.meta_key = 'tempo_di_realizzazione' AND CAST(wtdx_postmeta.meta_value AS CHAR) LIKE '%%' ) AND ( mt1.meta_key = 'portata' AND CAST(mt1.meta_value AS CHAR) LIKE '%contorno%' ) AND ( mt2.meta_key = 'difficolta' AND CAST(mt2.meta_value AS CHAR) LIKE '%%' ) ) AND wtdx_posts.post_type = 'ricetta' AND (wtdx_posts.post_status = 'publish' OR wtdx_posts.post_author = 1 AND wtdx_posts.post_status = 'private') GROUP BY wtdx_posts.ID ORDER BY wtdx_posts.post_date DESC LIMIT 0, 10" Nessuna Ricetta Trovata
0

Okay i found that at this point the taxonomy were not properly linked?Even if i have registered everything well,and i have even tried register_taxonomy_for_object_type.BTW

Page not found taxonomy custom post type here i've found my answer with this loop in functions.php

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.