Using Dynamic multiple attributes With WP_Query - php

I have a form from where clicks on the attribute and based on that click, I change my query to fetch relevant results
Below Array is generated dynamically based on user selection
Array
(
[tax_query] => Array
(
[0] => Array
(
[taxonomy] => pa_timing
[field] => slug
[terms] => Array
(
[0] => day
)
)
[1] => Array
(
[taxonomy] => pa_size
[fields] => slug
[terms] => Array
(
[0] => s
)
)
)
[post_type] => product
[post_status] => publish
[product_cat] => pads
[posts_per_page] => -1
)
Based on the above array WP_Query generated
SELECT vsrc_posts.* FROM vsrc_posts LEFT JOIN vsrc_term_relationships ON (vsrc_posts.ID = vsrc_term_relationships.object_id) LEFT JOIN vsrc_term_relationships AS tt1 ON (vsrc_posts.ID = tt1.object_id) LEFT JOIN vsrc_term_relationships AS tt2 ON (vsrc_posts.ID = tt2.object_id) WHERE 1=1 AND (
vsrc_term_relationships.term_taxonomy_id IN (351)
AND
tt1.term_taxonomy_id IN (259,263,274,314)
AND
tt2.term_taxonomy_id IN (348)
) AND vsrc_posts.post_type = 'product' AND ((vsrc_posts.post_status = 'publish')) GROUP BY vsrc_posts.ID ORDER BY vsrc_posts.post_date DESC
Array
(
[tax_query] => Array
(
[0] => Array
(
[taxonomy] => pa_timing
[field] => slug
[terms] => Array
(
[0] => day
)
)
[1] => Array
(
[taxonomy] => pa_size
[fields] => slug
[terms] => Array
(
[0] => s
)
)
)
[post_type] => product
[post_status] => publish
[posts_per_page] => -1
[product_cat] => pads
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[title] =>
[fields] =>
[menu_order] =>
[embed] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[post_name__in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[post_parent__in] => Array
(
)
[post_parent__not_in] => Array
(
)
[author__in] => Array
(
)
[author__not_in] => Array
(
)
[ignore_sticky_posts] =>
[suppress_filters] =>
[cache_results] => 1
[update_post_term_cache] => 1
[lazy_load_term_meta] => 1
[update_post_meta_cache] => 1
[nopaging] => 1
[comments_per_page] => 50
[no_found_rows] =>
[taxonomy] => pa_timing
[term] => day
[order] => DESC
)
But when I just hardcode the array and pass it to WP_Query it produces
SELECT vsrc_posts.* FROM vsrc_posts LEFT JOIN vsrc_term_relationships ON (vsrc_posts.ID = vsrc_term_relationships.object_id) LEFT JOIN vsrc_term_relationships AS tt1 ON (vsrc_posts.ID = tt1.object_id) LEFT JOIN vsrc_term_relationships AS tt2 ON (vsrc_posts.ID = tt2.object_id) WHERE 1=1 AND (
vsrc_term_relationships.term_taxonomy_id IN (351)
AND
tt1.term_taxonomy_id IN (274)
AND
tt2.term_taxonomy_id IN (348)
) AND vsrc_posts.post_type = 'product' AND ((vsrc_posts.post_status = 'publish')) GROUP BY vsrc_posts.ID ORDER BY vsrc_posts.post_date DESC
Array
(
[tax_query] => Array
(
[0] => Array
(
[taxonomy] => pa_timing
[field] => slug
[terms] => Array
(
[0] => day
)
)
[1] => Array
(
[taxonomy] => pa_size
[field] => slug
[terms] => Array
(
[0] => s
)
)
)
[post_type] => product
[post_status] => publish
[posts_per_page] => -1
[product_cat] => pads
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[title] =>
[fields] =>
[menu_order] =>
[embed] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[post_name__in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[post_parent__in] => Array
(
)
[post_parent__not_in] => Array
(
)
[author__in] => Array
(
)
[author__not_in] => Array
(
)
[ignore_sticky_posts] =>
[suppress_filters] =>
[cache_results] => 1
[update_post_term_cache] => 1
[lazy_load_term_meta] => 1
[update_post_meta_cache] => 1
[nopaging] => 1
[comments_per_page] => 50
[no_found_rows] =>
[taxonomy] => pa_timing
[term] => day
[order] => DESC
)
Which I expect WP_Query to produce. the problem is that WP_query is not producing for the dynamic query. Is there something I am missing
$dayArg = array(
'tax_query' => array(
array(
'taxonomy' => 'pa_timing',
'field' => 'slug',
'terms' => array( 'day' )
),
array(
'taxonomy' => 'pa_size',
'field' => 'slug',
'terms' => array( 's' )
)
),
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => - 1,
'product_cat' => 'pads'
)
This is argument I am passing to WP_Query one generating dynamically and one hardcoding it.

Related

Woocommerce: filter by attribute

I'm currently working on woocommerce, to make an specific request before displaying products. I'm using for that the hook woocommerce_product_query that is executed before the request is done in the class WC_Query in product_query method.
I can access the variable $q (query object), example bellow:
Now I would like to know how to filter this request by some custom attributes. I thought that would be something like that, but nothing is working maybe you could explain why.
add_action( 'woocommerce_product_query', 'filter_products_by_brand' );
function filter_products_by_brand( WP_Query $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'cb-brand',
'field' => 'slug',
'terms' => array( 'jeep' ),
'operator' => 'IN'
);
$q->set( 'tax_query', $tax_query );
}
WP_Query Object
(
[query] => Array
(
[product_cat] => accessoires-2/attelages/faisceaux
)
[query_vars] => Array
(
[product_cat] => faisceaux
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[title] =>
[fields] =>
[menu_order] =>
[embed] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[post_name__in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[post_parent__in] => Array
(
)
[post_parent__not_in] => Array
(
)
[author__in] => Array
(
)
[author__not_in] => Array
(
)
[orderby] => menu_order title
[order] => ASC
[meta_query] => Array
(
)
[tax_query] => Array
(
[relation] => AND
[0] => Array
(
[taxonomy] => product_visibility
[field] => term_taxonomy_id
[terms] => Array
(
[0] => 7
)
[operator] => NOT IN
)
)
[wc_query] => product_query
[posts_per_page] => 16
)
[tax_query] => WP_Tax_Query Object
(
[queries] => Array
(
[0] => Array
(
[taxonomy] => product_cat
[terms] => Array
(
[0] => faisceaux
)
[field] => slug
[operator] => IN
[include_children] => 1
)
)
[relation] => AND
[table_aliases:protected] => Array
(
)
[queried_terms] => Array
(
[product_cat] => Array
(
[terms] => Array
(
[0] => faisceaux
)
[field] => slug
)
)
[primary_table] =>
[primary_id_column] =>
)
[meta_query] =>
[date_query] =>
[queried_object] => WP_Term Object
(
[term_id] => 108
[name] => Faisceaux
[slug] => faisceaux
[term_group] => 0
[term_taxonomy_id] => 108
[taxonomy] => product_cat
[description] =>
[parent] => 106
[count] => 1
[filter] => raw
)
[queried_object_id] => 108
[post_count] => 0
[current_post] => -1
[in_the_loop] =>
[comment_count] => 0
[current_comment] => -1
[found_posts] => 0
[max_num_pages] => 0
[max_num_comment_pages] => 0
[is_single] =>
[is_preview] =>
[is_page] =>
[is_archive] => 1
[is_date] =>
[is_year] =>
[is_month] =>
[is_day] =>
[is_time] =>
[is_author] =>
[is_category] =>
[is_tag] =>
[is_tax] => 1
[is_search] =>
[is_feed] =>
[is_comment_feed] =>
[is_trackback] =>
[is_home] =>
[is_privacy_policy] =>
[is_404] =>
[is_embed] =>
[is_paged] =>
[is_admin] =>
[is_attachment] =>
[is_singular] =>
[is_robots] =>
[is_favicon] =>
[is_posts_page] =>
[is_post_type_archive] =>
[query_vars_hash:WP_Query:private] => a80e55b982d04f2e0de36fdd19a948d6
[query_vars_changed:WP_Query:private] =>
[thumbnails_cached] =>
[stopwords:WP_Query:private] =>
[compat_fields:WP_Query:private] => Array
(
[0] => query_vars_hash
[1] => query_vars_changed
)
[compat_methods:WP_Query:private] => Array
(
[0] => init_query_flags
[1] => parse_tax_query
)
)
Thanks.
If anyone wants to know.
To make an filter based on an attributes :
add_filter('woocommerce_product_query_tax_query', 'custom_product_query_meta_query', 10, 2);
function custom_product_query_meta_query( $tax_query, $query ) {
$taxonomy = 'pa_${NAME OF THE ATTRIBUTE}'; // Note: always start with "pa_" in Woocommerce
$tax_query[] = array(
'taxonomy' => $taxonomy,
'field' => 'name', // name or slug
'terms' => array( '1S026015002003' ),
'operator' => 'IN',
);
return $tax_query;
}

WP_Query no working when I use meta_query

I am trying to make a query that will loop through a custom post type. admin can decide if any post will show or not in the backend. I used used advance custom field to make the admin backend option. My admin custom 'show_on_catalog_page'
here is my query :
$song_query_args_debug = array(
'post_type' => 'songs',
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'show_on_catalog_page',
'value' => 'show',
'compare' => 'LIKE',
),
),
);
And here is my response :
WP_Query Object
(
[query] => Array
(
[post_type] => songs
[post_status] => publish
[posts_per_page] => 20
[paged] => 1
[meta_query] => Array
(
[0] => Array
(
[key] => show_on_catalog_page
[value] => show
[compare] => LIKE
)
)
)
[query_vars] => Array
(
[post_type] => songs
[post_status] => publish
[posts_per_page] => 20
[paged] => 1
[meta_query] => Array
(
[0] => Array
(
[key] => show_on_catalog_page
[value] => show
[compare] => LIKE
)
)
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[title] =>
[fields] =>
[menu_order] =>
[embed] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[post_name__in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[post_parent__in] => Array
(
)
[post_parent__not_in] => Array
(
)
[author__in] => Array
(
)
[author__not_in] => Array
(
)
[ignore_sticky_posts] =>
[suppress_filters] =>
[cache_results] => 1
[update_post_term_cache] => 1
[lazy_load_term_meta] => 1
[update_post_meta_cache] => 1
[nopaging] =>
[comments_per_page] => 50
[no_found_rows] =>
[order] => DESC
)
[tax_query] => WP_Tax_Query Object
(
[queries] => Array
(
)
[relation] => AND
[table_aliases:protected] => Array
(
)
[queried_terms] => Array
(
)
[primary_table] => wp_posts
[primary_id_column] => ID
)
[meta_query] => WP_Meta_Query Object
(
[queries] => Array
(
[0] => Array
(
[key] => show_on_catalog_page
[value] => show
[compare] => LIKE
)
[relation] => OR
)
[relation] => AND
[meta_table] => wp_postmeta
[meta_id_column] => post_id
[primary_table] => wp_posts
[primary_id_column] => ID
[table_aliases:protected] => Array
(
[0] => wp_postmeta
)
[clauses:protected] => Array
(
[wp_postmeta] => Array
(
[key] => show_on_catalog_page
[value] => show
[compare] => LIKE
[alias] => wp_postmeta
[cast] => CHAR
)
)
[has_or_relation:protected] =>
)
[date_query] =>
[request] => SELECT SQL_CALC_FOUND_ROWS DISTINCT wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id WHERE 1=1 AND (
( wp_postmeta.meta_key = 'show_on_catalog_page' AND wp_postmeta.meta_value LIKE '%show%' )
) AND wp_posts.post_type = 'songs' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 20
[posts] => Array
(
)
[post_count] => 0
[current_post] => -1
[in_the_loop] =>
[comment_count] => 0
[current_comment] => -1
[found_posts] => 0
[max_num_pages] => 0
[max_num_comment_pages] => 0
[is_single] =>
[is_preview] =>
[is_page] =>
[is_archive] =>
[is_date] =>
[is_year] =>
[is_month] =>
[is_day] =>
[is_time] =>
[is_author] =>
[is_category] =>
[is_tag] =>
[is_tax] =>
[is_search] =>
[is_feed] =>
[is_comment_feed] =>
[is_trackback] =>
[is_home] => 1
[is_404] =>
[is_embed] =>
[is_paged] =>
[is_admin] =>
[is_attachment] =>
[is_singular] =>
[is_robots] =>
[is_posts_page] =>
[is_post_type_archive] =>
[query_vars_hash:WP_Query:private] => 85b8ef9fff7842765383ddd6a149b375
[query_vars_changed:WP_Query:private] =>
[thumbnails_cached] =>
[stopwords:WP_Query:private] =>
[compat_fields:WP_Query:private] => Array
(
[0] => query_vars_hash
[1] => query_vars_changed
)
[compat_methods:WP_Query:private] => Array
(
[0] => init_query_flags
[1] => parse_tax_query
)
)
Can anyone give me any hints what's I am missing here?
I am trying to get the post of custom post type 'songs' where meta key 'show_on_catalog_page' value will be 'show'
and when I Turn on my wp_debug i got this result.
WordPress database error: [Not unique table/alias: 'wp_postmeta']
SELECT SQL_CALC_FOUND_ROWS DISTINCT wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id WHERE 1=1 AND ( wp_postmeta.meta_key = 'show_on_catalog_page' ) AND wp_posts.post_type = 'songs' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10
I have updated your code by below code, check it out now it should work with new wp_query,
$song_query_args_debug = array(
'post_type' => 'songs',
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'show_on_catalog_page',
'value' => 'show',
'compare' => '=',
),
),
);

Incorrect posts showing through WP-Query on WooCommerce

So basically I am using the following code which seems to be putting everything into the query perfectly however, I am still getting the wrong products. For example, I am requesting products with product category 'winter' but still receiving products in category 'all-season'
Here is my code:
add_action('pre_get_posts', 'advanced_search_query', 1000);
function advanced_search_query($query) {
if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
global $wp_query;
// category terms search
$categories = array('winter','all-season','summer','run-flat');
$category = $_GET['category'];
$categories = array_diff($categories,array($category));
$taxonomy = 'product_cat';
if (isset($category) && !empty($category)) {
$args = array(
'relation' => 'AND',
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $category,
'operator' => 'IN'
),
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => array($categories),
'operator' => 'NOT IN'
)
);
$query->set('tax_query', $args);
print_r($query);
}
return $query;
}
}
The print_r returns the following:
WP_Query Object ( [query] => Array ( [s] => 2457516 [post_type] => product ) [query_vars] => Array ( [s] => 2457516 [post_type] => product [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [paged] => 0 [comments_popup] => [meta_key] => total_sales [meta_value] => [preview] => [sentence] => [fields] => [menu_order] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [post_parent__in] => Array ( ) [post_parent__not_in] => Array ( ) [author__in] => Array ( ) [author__not_in] => Array ( ) [orderby] => menu_order title [order] => ASC [meta_query] => Array ( [0] => Array ( [key] => _visibility [value] => Array ( [0] => visible [1] => search ) [compare] => IN ) [1] => Array ( [key] => _stock_status [value] => instock [compare] => = ) ) [posts_per_page] => 12 [wc_query] => product_query [tax_query] => Array ( [relation] => AND [0] => Array ( [taxonomy] => product_cat [field] => slug [terms] => winter [operator] => IN ) [1] => Array ( [taxonomy] => product_cat [field] => slug [terms] => Array ( [1] => all-season [2] => summer [3] => run-flat ) [operator] => NOT IN ) ) ) [tax_query] => WP_Tax_Query Object ( [queries] => Array ( ) [relation] => AND [table_aliases:protected] => Array ( ) [queried_terms] => Array ( ) [primary_table] => [primary_id_column] => ) [meta_query] => [date_query] => [post_count] => 0 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 0 [max_num_pages] => 0 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => 1 [is_date] => [is_year] => [is_month] => [is_day] => [is_time] => [is_author] => [is_category] => [is_tag] => [is_tax] => [is_search] => 1 [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => [is_404] => [is_comments_popup] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => 1 [query_vars_hash:WP_Query:private] => ac61ad4fe3856f3f91e2ca85a7e667a1 [query_vars_changed:WP_Query:private] => [thumbnails_cached] => [stopwords:WP_Query:private] => [compat_fields:WP_Query:private] => Array ( [0] => query_vars_hash [1] => query_vars_changed ) [compat_methods:WP_Query:private] => Array ( [0] => init_query_flags [1] => parse_tax_query ) )
My guess is you have a double array for terms in your tax_query. You have:
$categories = array('...');
$args = array(
//....
array(
'terms' => array( $categories )
)
//....
);
This should just be:
'terms' => $categories

how to use association with TableRegistry in cakephp 3

As you can see in 1st Business\Model\Entity\Business Object there is category association present, when called in BusinessController,
But It's missing, when called in CategoriesController using TableRegistry. why that ? any idea how to get this fixed ?
I called called below in BusinessesController
$this->autoRender = false;
$this->paginate = [
'contain' => ['Cities', 'Categories' ]
];
foreach( $this->paginate( $this->Businesses ) as $business ) {
pr( $business );
}
I got this results
Business\Model\Entity\Business Object
(
[id] => 1
[city_id] => 414
[postal_id] => 780299
[slug] => coatings-2000-lloydminster-ab-ca-1
[business_name] => Coatings 2000
[street_address] => 2B Production Ave
[address_locality] => Lloydminster
[address_region] => AB
[postal_code] => T9V 0Y4
[last_modified] => Cake\I18n\Time Object
(
[time] => 2015-03-12T00:00:00+0530
[timezone] => Asia/Calcutta
[fixedNowTime] =>
)
[status] => 1
[categories] => Array
(
[0] => Business\Model\Entity\Category Object
(
[id] => 2276
[parent_id] => 2212
[category_slug] => protective-coatings
[category_name] => Protective Coatings
[lft] => 4449
[rght] => 4450
[level] => 2
[_joinData] => Cake\ORM\Entity Object
(
[category_id] => 2276
[business_id] => 1
[[new]] =>
[[accessible]] => Array
(
[*] => 1
)
[[dirty]] => Array
(
)
[[original]] => Array
(
)
[[virtual]] => Array
(
)
[[errors]] => Array
(
)
[[repository]] => BusinessesCategories
)
[[new]] =>
[[accessible]] => Array
(
[*] => 1
)
[[dirty]] => Array
(
)
[[original]] => Array
(
)
[[virtual]] => Array
(
)
[[errors]] => Array
(
)
[[repository]] => Business.Categories
)
)
[city] => Business\Model\Entity\City Object
(
[id] => 414
[city_slug] => lloydminster
[city_name] => Lloydminster
[[new]] =>
[[accessible]] => Array
(
[*] => 1
)
[[dirty]] => Array
(
)
[[original]] => Array
(
)
[[virtual]] => Array
(
)
[[errors]] => Array
(
)
[[repository]] => Business.Cities
)
[[new]] =>
[[accessible]] => Array
(
[*] => 1
)
[[dirty]] => Array
(
)
[[original]] => Array
(
)
[[virtual]] => Array
(
)
[[errors]] => Array
(
)
[[repository]] => Business.Businesses
)
when i called this in categories controller, it shows missing category associations
$this->autoRender = false;
$this->paginate = [
'contain' => ['Cities', 'Categories' ]
];
$businessTable = \Cake\ORM\TableRegistry::get( 'Business.Businesses' );
foreach( $this->paginate( $businessTable ) as $business ) {
pr( $business );
}
I got this results with missing categories associations
Business\Model\Entity\Business Object
(
[id] => 1
[city_id] => 414
[postal_id] => 780299
[slug] => coatings-2000-lloydminster-ab-ca-1
[business_name] => Coatings 2000
[street_address] => 2B Production Ave
[address_locality] => Lloydminster
[address_region] => AB
[postal_code] => T9V 0Y4
[last_modified] => Cake\I18n\Time Object
(
[time] => 2015-03-12T00:00:00+0530
[timezone] => Asia/Calcutta
[fixedNowTime] =>
)
[status] => 1
[categories] => Array
(
)
[city] => Business\Model\Entity\City Object
(
[id] => 1
[city_slug] => albany
[city_name] => Albany
[[new]] =>
[[accessible]] => Array
(
[*] => 1
)
[[dirty]] => Array
(
)
[[original]] => Array
(
)
[[virtual]] => Array
(
)
[[errors]] => Array
(
)
[[repository]] => Business.Cities
)
[[new]] =>
[[accessible]] => Array
(
[*] => 1
)
[[dirty]] => Array
(
)
[[original]] => Array
(
)
[[virtual]] => Array
(
)
[[errors]] => Array
(
)
[[repository]] => Business.Businesses
)
I guess that when you are in categories controller and you define contain for this, you are refering to the Categories model, not the Business one so that's why paginate is not taking the contain.
You could try this:
$this->paginate['Businesses'] = [
'contain' => ['Cities', 'Categories' ]
];

Custom search query

I've made a custom search form, a query using a custom post type with categories, custom taxonomy and meta fields and a search template for displaying them but cannot get the results to show. I'm using Wordpress. I've changed the way to make the query using pre_get_posts but have trouble now how to display the result. All variables are passed into query_vars.
Below is my code:
function my_get_posts( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
if (is_home() || is_search() || is_archive() || is_page( 'search' ) )
{
$query->set('post_type', 'yacht');
$query->set('posts_per_page', 1000);
$query->set('s', '' );
$meta_query = array();
$meta_query[] = array(
array(
'key' => 'yachts_length',
'value' => array($_GET['min_length'], $_GET['max_length']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_price',
'value' => array($_GET['min_price'], $_GET['max_price']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_year',
'value' => array($_GET['min_year'], $_GET['max_year']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
)
);
$meta_query['relation'] = 'AND';
$query->set('meta_query',$meta_query);
}
return $query;
}
add_action( 'pre_get_posts', 'my_get_posts');
And the query_vars output using the form:
Array
(
[cat] => 0
[page] => 0
[pagename] => search
[manufacturer] => 0
[min_length] => 3
[max_length] => 100
[min_price] => 500
[max_price] => 999999
[min_year] => 1970
[max_year] => 2015
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] => search
[static] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[comments_popup] =>
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[fields] =>
[menu_order] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[post_parent__in] => Array
(
)
[post_parent__not_in] => Array
(
)
[author__in] => Array
(
)
[author__not_in] => Array
(
)
[orderby] => menu_order
[order] => ASC
[post_type] => yacht
[posts_per_page] => 1000
[meta_query] => Array
(
[0] => Array
(
[0] => Array
(
[key] => yachts_length
[value] => Array
(
[0] => 3
[1] => 100
)
[compare] => BETWEEN
[type] => NUMERIC
)
[1] => Array
(
[key] => yachts_price
[value] => Array
(
[0] => 500
[1] => 999999
)
[compare] => BETWEEN
[type] => NUMERIC
)
[2] => Array
(
[key] => yachts_year
[value] => Array
(
[0] => 1970
[1] => 2015
)
[compare] => BETWEEN
[type] => NUMERIC
)
)
[relation] => AND
)
[ignore_sticky_posts] =>
[suppress_filters] =>
[cache_results] => 1
[update_post_term_cache] => 1
[update_post_meta_cache] => 1
[nopaging] =>
[comments_per_page] => 50
[no_found_rows] =>
)

Categories