How can i get post with multiple dynamic taxonomies - wordpress

I am filtering custom post types using ajax. I am able to filter using single taxonomy but how to filter results with multiple taxonomies.
This is the query i tried:
Code:
$args=array('orderby'=>'date','post_status'=>'publish');
//sort by bank if isset
if(isset($_POST['bank']))
{
$args['tax_query']=array(
array(
'taxonomy'=>'banks',
'field'=>'id',
'terms'=>$_POST['bank']
)
);
}
if(isset($_POST['card_type']))
{
$args['tax_query']=array(
'relation'=>'AND',
array(
'taxonomy'=>'cardtype',
'field'=>'id',
'terms'=>$_POST['card_type']
)
);
}
$query=new WP_Query($args);
But it only shows result with filtering from one taxonomy not both.

I think you are working on right track but a small mistake in argument may cause result to be rendered wrong.
Instead of 'relation'=>'AND', use 'relation'=>'OR'
'relation'=>'AND' will fetch result when both taxonomy condition match, while 'relation'=>'OR' condition will compare with any of the taxonomy.
click here to view the WP Query arguments
Actually yout array formation is wrong for tax_query.
Please check updated code below.
If you don't use $args['tax_query']['relation']= 'AND', then also it will work for you.
Please have a try with below code snippet.Update you code with below snippet.
$args = array(
'orderby'=>'date',
'post_status'=>'publish'
);
//sort by bank if isset
if(isset($_POST['bank'])){
$args['tax_query'][]= array(
'taxonomy'=>'banks',
'field'=>'id',
'terms'=>$_POST['bank']
);
}
if(isset($_POST['card_type'])){
$args['tax_query']['relation']= 'AND';//you can remove this
$args['tax_query'][]= array(
'taxonomy'=>'cardtype',
'field'=>'id',
'terms'=>$_POST['card_type']
);
}
$the_query = new WP_Query( $args );

Related

Wordpress Custom Post Type category used as homepage

I have a Custom Post Type called portfolio and a custom taxonomy portfolio_category that has a term called narrative.
I'm able to access the archive page using the URL /portfolio-category/narrative/.
I want the homepage to display all the items the same as on the narrative archive page without using a redirect.
I've added the following to functions.php
function custom_front_page($wp_query){
if($wp_query->get('page_id')==get_option('page_on_front')){
$wp_query->set('post_type','portfolio');
$wp_query->set('page_id',''); // empty
// fix conditional functions
$wp_query->is_page = false;
$wp_query->is_archive = true;
$wp_query->is_post_type_archive = true;
}
}
add_action('pre_get_posts','custom_front_page');
This is displaying all of the portfolio items on my homepage, but I would like it to be just showing the narrative items.
I've tried this in the php template file, but it isn't working either
<?php
$custom_query_args = array(
'post_type' => 'portfolio',
'portfolio_category' => 'narrative',
);
$custom_query = new WP_Query( $custom_query_args );
?>
How can I get just the narrative items for to show on the homepage?
You're on the right lines but you are not searching by custom taxonomy correctly. Searching by custom taxonomy is different than searching by categories.
If you take a look at the WP_Query documentation for searching by taxomony, you will see that you need to use tax_query to search posts for a custom taxonomy term.
You can use the code below in the template file you use for your homepage. The comments explain what each part does:
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => 5, // number of results to get, or -1 to get all of them
'tax_query' => array(
array( // important note, this is an array inside the tax_query arrays
'taxonomy' => 'portfolio_category', // your custom taxonomy
'field' => 'slug', // what to search, e.g. slug, term_id, name
'terms' => 'portfolio' // the term(s) to search for. If you want to search for multiple terms, you can use an array
)
)
)
// do a new query with your arguments
$portfolio_query = new WP_Query($args);
// if the query returns any results, loop through them and process them as required
if( $portfolio_query->have_posts() ):
while ( $portfolio_query->have_posts() ) :
$portfolio_query->the_post();
// do stuff with the post here, e.g. display the post...
endwhile;
endif;
// IMPORTANT! The custom query will overwrite the $post values that are used in the the_post etc.
// This restores the the current post in the main query - i.e. your homepage post.
wp_reset_postdata();

ACF - filter relationship query to display WooCommerce product available in this category

I'm trying to create a custom category product sort order in WooCommerce using Advanced Custom Fields.
I have found a great guide to set it up. The approach is:
Add the ACF relationship field to the category page.
Config it to display only products.
Add a function that orders the category page based on the products you've added.
It works great!
The problem I have is that it always displays all the available products and not only the product available in this category.
I'm trying to use this function but the tag_id is not working.
add_filter('acf/fields/relationship/query', 'my_acf_fields_relationship_query', 10, 3);
function my_acf_fields_relationship_query( $args, $field, $post_id ) {
$args['tag_id'] = $category_id;
return $args;
}
Any suggestions?
You want to filter by product category terms, so you should not use:
[tag_id]
But instead use:
[term_id]
You follwed the tutorial, but the filter function is not working. You have created the custom field and inserted the function to create your custom product order. What is not working, if I got your question the right way, is the function that fetches an array of all the product IDs within a specific category. So you are getting all the available products and not the ones of the category you selected.
This should have been part of your question so others can find a solution:
This is the code from the tutorial you used: https://www.igoo.co.uk/2016/10/setting-a-custom-category-specific-product-sort-order-in-woocommerce-using-advanced-custom-fields/
function my_custom_product_order($q) {
if(!is_admin())
{
// fetch current category id from active query
$category_id = $q->get_queried_object_id();
// get array of all product IDs in current category
$product_ids = get_category_product_ids($category_id);
// get preferred order from ACF field
$product_ids_order_preferred = get_field('product_order', 'product_cat_' . $category_id);
// if we have some product sort order set…
if($product_ids_order_preferred)
{
// merge our preferred category ids array with the array of all products ids, and remove duplicates
$product_ids = array_unique(array_merge($product_ids_order_preferred, $product_ids));
}
// set the 'posts__in' argument to the new array of post IDs (unfortunately wordpress doesn’t let you just pass an array of IDs straight in here)
$q->set('post__in', $product_ids);
// set the query orderby value to observe the posts__in field
$q->set('orderby', 'post__in');
}
remove_action('woocommerce_product_query', 'custom_pre_get_posts_query');
}
add_action('woocommerce_product_query', ‘my_custom_product_order’);
In Line 7 of the code above, you are using the "get_category_product_ids($category_id)" function and give the $category_id as a parameter.
The "get_category_product_ids" function in your tutorial looks like this:
// helper function to fetch all product IDs from a specific category ID
function get_category_product_ids($category_id) {
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'fields' => 'ids',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $category_id,
'operator' => 'IN'
)
)
);
$ids = get_posts($args);
return $ids;
}
This function gets the parameter $category_id and uses it to make a post query and saving the resulting ids of the tax query.
This is the actual answer, which might help you:
If you want to find out why it is not working, you should first have a look at the $category_ id variable, because this value is the most important to make it work. So you can have a look if there is data inside the variable with var_dump inside your function after setting the variable:
var_dump($category_id)
Is the variable set correctly and do have data when the "my_custom_product_order" function is called? You should get the id of the current viewed category.
If you not get an id inside my_custom_product_order
you should have a look at the "get_queried_object_id()" function. It is possible to get this value with another way. The "get_queried_object()" function returns a WP_Term object. So you can also do:
$category_id = $q->get_queried_object();
$category_id = $category_id->term_id;
Maybe you now have the id you need.
If not, you should check the parameter $q, what is it for? Maybe this could cause the problem. You can try something like:
if (is_product_category()) {
$q = get_queried_object();
$term_id = $q->term_id;
}
If you are getting an id inside my_custom_product_order
then you should have a look at the "get_category_product_ids" function. Does the parameter work? Try var_dump inside this function and see if there is value handed over to the function. If not, you could get the variable another way. This seems redundantly and makes the parameters useless, but this way you can make sure that there is a category ID for your tax query. Put this at the start of your "my_category_product_ids":
$category_id = get_queried_object();
$category_id = $category_id->term_id;
Hope this will help you finding out why it's not working. Without knowing that it will be hard to fix the problem. If I'm right, the problem is with your $category_id not having a value for making the functions work.

Accessing Advanced Custom Fields by Page Name

I am trying to retrieve all advanced custom fields tied to a particular page. This is different than iterating through posts, I am familiar with the following:
$posts = get_posts(array(
'post_type' => 'post_name',
'meta_key' => 'color',
'meta_value' => 'red'
));
However this method is specific to posts and does not allow me to retrieve all ACF by page name.
I appreciate any suggestions on how to accomplish this.
The are too ways to do this that come to mind...
1. Using the Loop
Using WP_Query you can do something like this...
<?php
// WP_Query arguments
$args = array (
'pagename' => 'homepage',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
the_field( "field_name" );
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
In place of the 'homepage' in 'pagename' => 'homepage', you want to put the page slug of your page. And of course in place of the_field( "text_field" ); you want to add your fields/content.
You can also query by Page ID and some other parameters. You can find all other parameters that you can use here:
https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
2. Adding second parameter to the_field() function
More simple way, without custom loop, is just to use ACF's built-in the_field() function with the $post->ID parameter added as a second parameter.
<?php the_field('field_name', 123);
This might be the way to go since you want to show the content of only one page, and therefore don't really need to loop.
Reference: http://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/
You can use ACF's get_fields() function -
<?php $fields = get_fields( $post->ID ); ?>
You could then loop through them or simply print the array for testing.
http://www.advancedcustomfields.com/resources/get_fields/

Wordpress query_posts with ID and slug combination

is it possible to use query_posts with a combination of id's and slugs?
I have an array of id's and slugs from user input and want to know if I can use the two in the same post query, or do I have to convert the slugs into their respective post ID's before and then use posts__in?
I have the following mixture of slugs and ID's in an array…
$values = array('this-is-a-test-slug', '2345', '4510', 'another-slug-here', '8934');
How can I use this in query_posts? Can I at all?
query_posts(array('post_type' => 'post', 'post__in' => $values, 'orderby' => 'rand'));
I know that post__in works ok with numeric ID's but I don't think slugs work here as it expects a numerical array.
Thanks
If you're doing something like this, i don't see why it wouldn't be a problem to just convert them all over to ID? There's a thread that sort of helps, and I've written some code (with help from that link) that might help you get started
function allToID($array){
$id = array();
foreach($array as $convert):
if(is_numeric($convert)):
continue; // skip if it's already an ID
else:
$the_slug = $convert;
$args=array(
'name' => $the_slug,
'numberposts' => 1
);
// Ask wordpress to get this post
$my_posts = get_posts($args);
if( $my_posts ) :
// push onto our new array of only IDs
array_push($id, $my_posts[0]->ID);
else continue;
endif;
endif;
endforeach;
}
Ideally you'll be able to run post__in => alltoID($values)
Hope this helps!

how do i enter a var in a wordpress custom query?

how do I put a custom var in the following line of the below
AND $wpdb->postmeta.meta_value = 'email'
I want to put in a var for 'email'
something like
AND $wpdb->postmeta.meta_value = $var
anyone know how to do this is Wordpress. I think i need to bind?
re: http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query
It depends a bit on context, but if you really are performing a custom query (as, say, part of a widget) you would provide it as an argument to WP_Query:
$query = new WP_Query( array( 'meta_value' => 'user#example.com' ) );
Oftentimes this would include a corresponding meta_key in that argument array.
If you want to modify the currently running query (say, on a category page, only show posts that match a certain criteria) you would perform that modification during the pre_get_posts action. Recommended reading: Andrew Nacin's You Don't Know Query.
I had done that using custom query plugin Go here
else custom your query like this
<?php
$args = array('post_type' => 'page','meta_query' => array(array('key' => 'email','value' => 'yes','compare' => '%')));
$var = new WP_Query($args);
// The Loop
while ( $var->have_posts() ) : $var->the_post();
$P_ID = get_the_ID();
endwhile;
// Reset Post Data
wp_reset_postdata();
Hope it helps

Resources