Reputation: 455
I am trying to create the below function in my theme's function.php file and call it from my taxonomy.php file via
query_brands_geo('dealers', 'publish', '1', $taxtype, $geo, $brands);
all variables are set in taxonomy.php.
The below query works perfect if I put it directly in my taxonomy.php file. What am I missing to make this work as a function?
As a function I get this error statement for argument repeated for 2-6:
Warning: Missing argument 2 for query_brands_geo()
function query_brands_geo($posttype, $poststatus, $paidvalue, $taxtype, $geo, $brands) {
/* Custom Query for a brand/geo combination to display dealers with a certain brand and geography */
//Query only for brands/geography combo and paid dealers
$wp_query = new WP_Query();
$args = array(
'post_type' => '$posttype',
'post_status' => array($poststatus),
'orderby' => 'rand',
'posts_per_page' => 30,
'meta_query' => array(
array(
'key' => 'wpcf-paid',
'value' => array($paidvalue),
'compare' => 'IN',
)
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => $taxtype,
'field' => 'slug',
'terms' => $geo
),
array(
'taxonomy' => 'brands',
'field' => 'slug',
'terms' => $brands
)
)
);
$wp_query->query($args);
}
add_action( 'after_setup_theme', 'query_brands_geo' );
Upvotes: 0
Views: 250
Reputation: 2249
I have just tested this on a WP site and it works so it should for you too if you respect the same logic:
function query_brands_geo($posttype, $poststatus) {
$args = array(
'post_type' => $posttype,
'post_status' => array($poststatus)
);
$result = new WP_Query($args);
wp_reset_query();
return $result;
}
$result = query_brands_geo('post', 'published');
while ( $result->have_posts() ) : $result->the_post();
the_content();
endwhile; // end of the loop.
Upvotes: 0
Reputation: 2249
OK I tested in and it works like this : Correct your function code like this :
function query_brands_geo($posttype, $poststatus, $paidvalue, $taxtype, $geo, $brands) {
/* Custom Query for a brand/geo combination to display dealers with a certain brand and geography */
//Query only for brands/geography combo and paid dealers
$args = array(
'post_type' => '$posttype',
'post_status' => array($poststatus),
'orderby' => 'rand',
'posts_per_page' => 30,
'meta_query' => array(
array(
'key' => 'wpcf-paid',
'value' => array($paidvalue),
'compare' => 'IN',
)
),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => $taxtype,
'field' => 'slug',
'terms' => $geo
),
array(
'taxonomy' => 'brands',
'field' => 'slug',
'terms' => $brands
)
)
);
query_posts($args);
}
Then in taxonomy.php, go like this :
query_brands_geo('post', 'published', ..., ..., ...);
while ( have_posts() ) : the_post();
the_content(); // Etc.
endwhile; // end of the loop.
Upvotes: 0
Reputation: 2249
You need to return something from your function, so change this at the end of the function code:
$wp_query->query($args);
with this :
$result = $wp_query->query($args);
wp_reset_query();
return $result;
And call it appropriately in taxonomy.php, knowing that the result object of the function, when called like this :
$myquery = query_brands_geo($arg1, $arg2, ...); // Etc.
Will be a query result object, so you have to loop through it, etc. (see the WordPress code elsewhere with loops).
Upvotes: 0
Reputation: 2249
You say you want to use this function that you place in functions.php and have it executed in your taxonomy.php file. Right, but the code you have posted shows that you are executing it as an action (the function code is followed by : add_action(...
). So what you have done there is that you have hooked your function to the after_setup_theme
hook. So the function is not getting executed by the call you have (I suppose) placed in taxonomy.php, it is actually automatically executed by WP after the theme setup, when do_action('after_setup_theme'...)
is executed by WordPress! And in this context, only one argument is passed, so you get the error message : argument 2 is missing...
So take out the add_action(...)
line, keep your function in functions.php and just normally call it in taxonomy.php. It should work.
Upvotes: 0