I'm using this tut to create an AJAX filter with multiple custom taxonomies. Everything works fine with the filter, that's not the issue.
Now I want to show the selected taxonomies on the result. The filter creates a different query every time depending on your selection, so it queries tax x, or tax x,y, or whatever. I want to show those queried tags on the results.
With get_terms(); I get all the terms that the queried posts have, but I only want the ones in the $args
The closest I've been it's with $query->request; where I get something like ( ( lv_term_relationships.term_taxonomy_id IN (5) ) AND ( tt1.term_taxonomy_id IN (21,27) ) )
Can I get the names of those IDs from $query->request;? Is there any other way of displaying the $args ?
Thank you in advance
Related
I am trying to get categories names for the ones that are available in permalink. e.g. I have the following permalink structure.
abc.com/product-category/category-1/category-2/
Where as category-1 and category-2 both are categories. When I try $wp_query->get_queried_object(), it just returns me info related to category-2. How can I get details of category-1 using any existing wordpress functions?
Thanks
As the previous categories are parent of the current use can get it using get_term https://developer.wordpress.org/reference/functions/get_term/
$child_category = $wp_query->get_queried_object();
$parent_category = get_term($child_category->parent, 'product-category');
You could get the parent of the category-2 using the get_term function.
$child_term = get_queried_object();
$parent_term = get_term($child_term->parent, 'product_cat'); // if your using Woocommerce then the taxonomy is called 'product_cat'
Both of these variables will be WP_Term objects.
https://developer.wordpress.org/reference/classes/wp_term/
From there you can access the name, id and any other information you need.
Revised
If you're wanting to create a custom category page (i.e. not a Woocommerce/WordPress page) then I would recommend adding a meta-box to the page editor.
https://developer.wordpress.org/reference/functions/add_meta_box/
https://www.advancedcustomfields.com/
Have a select box with your product categories.
https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
Then in your page template, use the term(s) ID as part of your WP_Query to pull products from both of those product categories.
I'd like to get something like this:
Frontend results
I have a CPT called "Property" and every property have a meta value "owner" (HR19 / Other) and i'd like to show them separated by "owner" with a heading like the image attached and I need to paginate them every 9 pages.
Thanks in advance.
What I understand your meta key is "owner", if that is correct meta keys are "HR19" and "Other".
If above info is correct why don't you try to separate both sections by separate queries.
Suppose prepare one section for HR19 and query for that will be,
$query = new WP_Query( array( 'meta_key' => 'owner', 'meta_value' = 'hr/19', 'meta_compare' => '=' ) );
Write down the loop and after loop use wp_reset_postdata. Similarly go for other meta value and write down custom query.
Regarding pagination you can use separate paginations for both section and use ajax to get posts for that particular section.
Here is a nice post for getting posts via ajax. Hope this helps. If requirement is different then let me know in comments.
i have a wordpress query like this
query_posts( 'cat=100&posts_per_page=3&meta_key=views&orderby=date,meta_value&order=DESC' );
in this code , i slected last 3post of special category 100 ( ordered by values of views meta key
i need to run this code out of wordpress area ( out of theme or plugins ) so i need to use mysql_query
i tried a lot to join category , but i can't do it
here is details of WordPress_Taxonomy database relations and query_posts
https://codex.wordpress.org/WordPress_Taxonomy
http://codex.wordpress.org/Function_Reference/query_posts
I have 5 blog sections each representing a category. However, I would like to have a category called "all" that if checked would include the post in all 5 section without having to click every category.
I guess the only way to do this is to add something into the blog category template file to tell it to include the usual category posts as well as the "all" category.
now I think i've located where it queries the post:
if ( $wp_query->have_posts()) : while (have_posts()) : the_post();
but there is nothing identifying the current category id, except for a few things above for specifying the right title for the theme.
What would be the best way of achieving what I have explained?
You can interact with the wp_query object... the same way that you interact with query_posts()... it accepts the same arguments.
For either you can use Category Parameters as follows (from the wordpress codex: query_posts function reference)
Category Parameters
Show posts associated with certain categories.
cat (int) - use category id.
category_name (string) - use category
slug (NOT name).
category__and (array) - use category
id.
category__in (array) - use category
id.
category__not_in (array) - use
category id.
When it references $wp_query, the variables of the query have already been defined, you can set up your own query with any parameteres you want.
I'm using a custom taxonomy for my posts which I print like this:
the_terms(get_the_ID(), 'sizes', '<p>', ', ', '</p>');
This works fine but it prints the terms in the order they were added, is there a way to make it print alphabeticaly? the_terms function doesn't let me pass any order parameter so I guess I have to use a different one but I haven't found any yet.
Thanks in advance.
the_terms() is using get_term_list, to retrive the taxonomy ( http://core.trac.wordpress.org/browser/tags/3.0.4/wp-includes/category-template.php#L932 ) for the code.
You have the option to use a filter here, but it is applyed to the HTML generated for every single term (taxonomy1). Not the best option to sort.
Best is use the function get_the_terms() , sorting the results and constructing the output list by yourself!