I need to query posts from custom post taxonomy (set by user from WordPress theme option panel).
I am using the following code:
<?php
if ( function_exists( 'get_option_tree') ) {
$taxonomy = get_option_tree( 'taxonomy_option' );
}
$args = array(
'project_type' => $taxonomy,
'show_count' => 6,
);
query_posts($args);
?>
The code is working just if I enter on 'project_type' => 'my taxonomy name'. What is wrong with the code above? How I can get an option (option-tree) into args array? Later edit: the optiontree function render the taxonomy ID.
thanks
The function get_option_tree() might be giving you an array, you should check it out with
echo "<pre>".print_r($taxonomy,true)."</pre>";
Update:
After some discussion with #Ad Reactor, we found out that the $taxonomy output was a number instead of a slug. To get the slug, we can use
$term = get_term( 1234, 'project_type' );
$slug= $term->slug;
where 1234 is replaced with the real term_id
The only problem I can see is $taxonomy is not initialised and because of that if your condition does not met the $taxonomy will be undefined.
So define $taxonomy above your if condition.
$taxonomy = '';
if ( function_exists( 'get_option_tree') ) {
.....
Related
I would like to use a shortcode in WordPress to output all the categories that a post is tagged to. With the PHP code I created, all categories are now displayed. How can I get it to display only those. Which are used in the post on what the shortcode is included? Thanks for your help!
function get_cat($post_id) {
$terms = get_terms( array(
'taxonomy' => 'test',
'hide_empty' => true,
) );
echo '<br><strong>Test: </strong>';
foreach ($terms as $term){
echo '<a target="_blank" href="/test/'. $term->slug . '/">' . $term->name . '</a>';
echo ' ';
}
}
add_shortcode( 'get_cat', 'get_cat' );
If you want to get the categories of the current post in which the shortcode was used then you can change the function where you get your categories which in your case is the get_terms with get_the_terms() function.
Solution:
Change this:
$terms = get_terms( array(
'taxonomy' => 'test',
'hide_empty' => true,
) );
To this:
$terms = get_the_terms(get_the_ID(), 'category');
The second parameter is taxonomy, change to your desired taxonomy I tested it with the default one which is 'category', in your case it is 'test' based on your code.
With this change, you'll be able to see only categories that are used on that particular post.
Other solution:
If you want to use the default taxonomy (category), you can use this function get_the_category()
With this solution you'd have to pass only the post id as a parameter like this:
$terms = get_the_category(get_the_ID());
Note: This function only returns results from the default "category" taxonomy. For custom taxonomies use the first solution with get_the_terms() function.
I have a structured my blog posts to have categories, and the slugs of these categories are identical to some custom-post post titles.
I want to run a link from the post to the custom-post page using these matching slugs.
In single.php I am trying to run this code... but instead of returning the information of the custom-post it returns the information of the current post
<?php
$categories = get_the_category();
if ( ! empty( $categories ) ) {
foreach( $categories as $category ) {
$current_slug = $category->slug;
$args = array(
'post_type' => 'community',
'name' => $current_slug
);
$cat_query = new WP_Query($args);
if ( $cat_query->have_posts() ) {
the_title( sprintf( '<h3 class="post-title">', esc_url( get_permalink() ) ), '</h3>');
wp_reset_postdata();
}
}
}
?>
My hope for this code was -> if a post is given a category with the slug: 'cat-one' then at the top of the post would be a link to the CAT ONE page (which is a custom post type page with a url ..../communities/cat-one.
This should absolutely be possible. There's a few issues with your code however, and I've cleaned up a few other things.
You've got the code set up to loop through the categories, but you're using name in your WP_Query() args, and don't have a loop set up for the results. According to the WP_Query() docs, name will return a single post - if you want more than one, (since WP 4.4) you can use post_name__in instead. Also, and namely the larger issue with your code, is that you're never set up the internal variables inside your $cat_query. I've also added a while loop so it will spit out multiple category links.
Without defining the internal variables with the $query->the_post() method (or passing a $post_id argument to them), the post variable functions like the_title() and get_permalink() will use the current post, which is what your'e seeing now.
This should get you started:
if( $categories = get_the_category() ){
foreach( $categories as $category ){
$args = array(
'post_type' => 'community',
'post_name__in' => $category->slug
);
$cat_query = new WP_Query( $args );
if( $cat_query->have_posts() ){
while( $cat_query->have_posts() ){
$cat_query->the_post();
the_title( sprintf( '<h3 class="post-title">', esc_url( get_permalink() ) ), '</h3>');
}
wp_reset_postdata();
}
}
}
I have a custom post type advice and taxonomy adcat. I want to display all post belong to that category.
Lets say I have 4 categories namely : 'games', 'tours', 'dishes', 'hotels' and also this four category is a menu. If I click one of the category for example: hotels all post belong to the hotels should display.
By the way this code I used to show wordpress default categories:
<?php $catname = wp_title('', false); ?>
<?php $posts = get_posts("category_name=$catname&numberposts=8&offset=0");
foreach ($posts as $post) : start_wp(); ?>
//html output
<h1><?php the_title(); ?></h1>
<?php endforeach; ?>
this is not working in custom post 'taxonomies' any suggestion would be helpful thank's
try this maybe it work... I write a note so you can see whats going on.. hope it help
<?php
// Get the term/category of the post
$terms = get_the_terms( $post->ID , 'advice-cat' );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'advice cat' );
}
//WordPress loop for custom post type
$terms = get_the_terms( $post->ID , 'advice-cat' );
$my_query = new WP_Query('post_type=advice&advice-cat=' . $term->name . '&posts_per_page=-1');
while ($my_query->have_posts()) : $my_query->the_post(); ?>
// output content
<?php the_title(); ?>
<?php endwhile; wp_reset_query(); ?>
try something like this, this is the example to fetch post by category id
$args = array( 'cat' => $cat_id, 'post_type' => 'advice', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
endwhile;
wp_reset_postdata();
you can also use with category name as well.
$args = array('category_name' => 'catname', 'post_type' => 'advice', 'posts_per_page' => -1 );
you can use post_per_page as per your requirement, I have just added -1 for all the posts
I don't really understand your terminology. When you are talking about adcat, is it a custom taxonomy or a term of the build-in taxonomy category. If adcat is a custom taxonomy, you should use the tax_query in WP_Query,not the category parameters.
Remember, category and a custom taxonomy are both taxonomies, their direct children is called terms, and their direct childen is called child terms
You should also not be using wp_title() to get the queries object. You should be using get_query_var() to get the queried object. For categories it will be cat, taxonomy will be taxonomy and for terms term. Have a look at get_categories, get_taxonomies and get_terms for returned values
Example
$category = get_query_var( `cat` );
$cat_slug = $category->slug;
You can now return $cat_slug to your custom query as category_name
EDIT
I've quickly rethinked the whole thing and checked your comment as well. Why not just copy your index.php and rename it taxonomy.php. There is no need at all for a custom query here. The default loop in taxonomy.php should do it
EDIT 2
For further reading, go and check the following articles
Theme Development
Template Hierarchy
Not sure if I read your answer correctly but what I am assuming is you want to search posts by the taxonomy terms correct? So in your adcat taxonomy you have 'games', 'tours', 'dishes', 'hotels' as your terms or categories. Your best option would be to use a tax_query in your query arguments. Here is how to do that with the WP_Query() object.
<?php
$args = array(
'post_type' => 'advice',
'posts_per_page' => 8,
'tax_query' => array(
'taxonomy' => 'adcat',
'terms' => array('games', 'tours', 'dishes', 'hotels'),
'field' => 'slug'
)
);
$query = new WP_Query($args);
if($query->have_posts()) : while($query->have_posts()) : $query->the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
Important Note:
When using a WP_Query you will be overwriting the default post data. So in order to get that data back you just use the wp_reset_postdata() as you can see in the example above after the loop has finished.
I'm sure this is going to be a silly question... but here it goes.
I currently have a page that only displays posts of a Custom Post Type (car).
I do this by running a query
$args = array(
'post_type' => 'car');
$query = new WP_Query( $args );
On to the loop...
For this Custom Post Type i have a Custom Taxonomy e.g. Subaru, Honda etc...
I'm just trying to work something else out, but if I wanted to show only posts that are Subaru's, how would I query that?
I guess I want to query the 'slug' (subaru), this code doesn't work, but you can see the route I was heading...
$args = array(
'name' => 'subaru',
'post_type' => 'car');
$query = new WP_Query( $args );
On to the loop...
I know name isn't right. What is the correct term to add to my $args array?
Many thanks
Depends on what your taxonomy is called. In my example its called 'brands':
$args = array(
'post_type' => 'car',
'brand' => 'subaru'
);
$query = new WP_Query( $args );
see http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
What you're trying to do is a taxonomy query. I could tell you how to do that but I think it's important I point out a much bigger mistake first. You shouldn't querying this on a page.
That's what archives are for.
Create a template file called archive-car.php and output there instead. That removes the need to run a custom WP Query.
Then create another file taxonomy-{your-custom-taxonomy-name}.php
Output the cars there as well and your problem is solved without adding any inefficient queries.
I think you need to use get_terms()
$terms = get_terms("Subaru");
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
echo "<ul>";
foreach ( $terms as $term ) {
echo "<li>" . $term->name . "</li>";
}
echo "</ul>";
}
See this http://codex.wordpress.org/Function_Reference/get_terms
I am creating a custom theme for woocommerce and I need to be able to create a mini product display. I am having problems finding documentation on the woocommerce api. I have a comma delimited list of product IDs that I need to iterate through and display a custom mini product display for each in sequence.
$key_values = get_post_custom_values('rel_products_ids');
//get comma delimited list from product
$rel_product_ids = explode(",", trim($key_values, ","));
// create array of just the product ids
foreach ( $rel_product_ids as $pid ) {
//sequentially get each id and do something with it
$loop = new WP_Query( array( 'post__in' => $pid ) );
// also tried ...
//$loop = new WP_Query( array( 'ID' => $pid ) );
while ( $loop->have_posts() ) : $loop->the_post(); $_product = &new WC_Product( $loop->post->ID );
//do stuff here I have stripped the html in favor of getting to the meat of the issue
woocommerce_show_product_sale_flash( $post, $_product );
if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_single');
get_permalink( $loop->post->ID );
the_title();
$_product->get_price_html();
endwhile;
}
Any help would be appreciated.
Thank you,
Tim
Use this method:
$_product = wc_get_product( $id );
Official API-docs: wc_get_product
Another easy way is to use the WC_Product_Factory class and then call function get_product(ID)
http://docs.woothemes.com/wc-apidocs/source-class-WC_Product_Factory.html#16-63
sample:
// assuming the list of product IDs is are stored in an array called IDs;
$_pf = new WC_Product_Factory();
foreach ($IDs as $id) {
$_product = $_pf->get_product($id);
// from here $_product will be a fully functional WC Product object,
// you can use all functions as listed in their api
}
You can then use all the function calls as listed in their api:
http://docs.woothemes.com/wc-apidocs/class-WC_Product.html
Alright, I deserve to be throttled. definitely an RTM but not for WooCommerce, for Wordpress.
Solution found due to a JOLT cola (all hail JOLT cola).
TASK:
Field named 'related_product_ids' added to a custom post type. So when that post is displayed mini product displays can be displayed with it.
PROBLEM:
Was having a problem getting the multiple ids returned via WP_Query.
SOLUTION:
$related_id_list = get_post_custom_values('related_product_ids');
// Get comma delimited list from current post
$related_product_ids = explode(",", trim($related_id_list[0],','));
// Return an array of the IDs ensure no empty array elements from extra commas
$related_product_post_ids = array( 'post_type' => 'product',
'post__in' => $related_product_ids,
'meta_query'=> array(
array( 'key' => '_visibility',
'value' => array('catalog', 'visible'),'compare' => 'IN'
)
)
);
// Query to get all product posts matching given IDs provided it is a published post
$loop = new WP_Query( $related_posts );
// Execute query
while ( $loop->have_posts() ) : $loop->the_post(); $_product = get_product( $loop->post->ID );
// Do stuff here to display your products
endwhile;
Thank you for anyone who may have spent some time on this.
Tim
global $woocommerce;
var_dump($woocommerce->customer->get_country());
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = new WC_product($cart_item['product_id']);
var_dump($product);
}