I'm trying to create a loop that loads posts from a custom taxonomy and a specific category , i have used this code below but it doesn't show anything
<ul class="row">
<?php
$latest_apps = new WP_Query(array(
'post_type' => array('product','post'),
'posts_per_page' => 30,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array( get_option("free_apps_category") )
),
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => array( get_option("premium_apps_category") )
)
)
));
$c = 0;
while ($latest_apps->have_posts()) : $latest_apps->the_post();
$c++;
// post is even
if( $c % 2 == 0) {
?>
<?php the_post_thumbnail("post"); ?>
</li>
<?php
}
// post is odd
else {
?>
<li class="content-box-post">
<?php the_post_thumbnail("post"); ?>
<?php
}
endwhile;
?>
</ul>
i have used static id numbers instead of get_option('free_apps_category') and get_option('premium_apps_category') but it still does not work
please help , thank u so much
regards
If it does not show you nothing, it is because you have no posts that match your condition.
And reading the code fast One can see that your query is for a post either normal or under product post type that is both on the premium_apps_category AND free_apps_category . Are you sure that you actually have one ?
Related
Hello stackoverflow community.
I am trying to get functionality to display a product counter but only in subcategories. In the main categories I want to disable it.
At the moment I have been able to do this:
function add_product_count_view()
{
global $wp_query;
$category_id = $wp_query->get_queried_object()->term_id;
$query = new WP_Query(array(
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $category_id,
'include_children' => true,
),
),
'nopaging' => true,
'fields' => 'ids',
));
if (function_exists("is_shop") && $category_id != 0) {
echo '(' .esc_html($query->post_count) . ')';
}
}
and output
<span class="custom-home-category-counts">
<?php do_action('count_product_title');?>
</span>
This way I got what I want but the counter shows the same value for each subcategory
----UPDATE---- #CBroe
It is possible that all this code is garbage. My knowledge of PHP is quite poor. Generally what I mean is, I want to disable the product counter in main categories and leave only in all subcategories.
This is the original code that renders the product counter.
<?php if (get_theme_mod('category_show_count', 1)) : ?>
<p class="is-xsmall uppercase count">
<?php if ($category->count > 0) {
echo apply_filters('woocommerce_subcategory_count_html', $category->count . ' ' . ($category->count > 1 ? __('Products', 'woocommerce') : __('Product', 'woocommerce')), $category);
}
?>
</p>
<?php endif; ?>
After every custom WP_Query, you need to run wp_reset_postdata() to ensure that global $post is restored to the current post in the main query.
In your code, you can use this function as follows:
function add_product_count_view()
{
global $wp_query;
$category_id = $wp_query->get_queried_object()->term_id;
$query = new WP_Query(array(
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $category_id,
'include_children' => true,
),
),
'nopaging' => true,
'fields' => 'ids',
));
if (function_exists("is_shop") && $category_id != 0) {
echo '(' .esc_html($query->post_count) . ')';
}
wp_reset_postdata(); //reset the main query loop
}
This way, the next time the query runs, it runs with new data. This can be the reason why the counter shows same value for all sub categories.
(first of all i'm new with php)
i have a wordpress blog where i have written about some ppl and i added their names as tags in a costum taxonomy to avoid multi language field with polylang.
I was looking for a loop that allow to show, for every tag, the title of post where tag exist.
Example => post's name: "what a chef can do?" (tagged Alex P.)
result: -Alex P. "what a chef can do?" 05/10/2020 - "Tomatos in the world" 15/12/2020.
For every person(tag) i have to make an Accordion.
i've started to write code but i'm not looking for a solution and i'm getting lost.
<?php $tag_args = array(
'orderby' => 'title',
'order' => 'ASC'
);
?>
<ul id="Genre-List">
<li>DEFAULT</li>
<?php
$terms = get_terms(
array(
'taxonomy' => 'chef',
'hide_empty' => false,
)
);
// Check if any term exists
if ( ! empty( $terms ) && is_array( $terms ) ) {
// Run a loop and print them all
print_r($terms);
foreach ( $terms as $term ) {
?>
<li><a href="<?php echo esc_url( get_term_link( $term ) ) ?>">
<?php echo $term->name; ?>
</a></li>
<?php
}
}
?>
Thanks if anyone can help about this.. im really getting mad.
If you are looking to get all posts which have any custom taxonomy term associated with it, you will need to query posts using a tax_query rather than simply retrieve all of the terms.
// get 'chef' taxonomy
$terms = get_terms([
'taxonomy' => 'chef',
'hide_empty' => false,
]);
// get any posts with one of the 'chef' taxonomy terms associated with it
$query = new WP_Query([
'post_type' => 'post',
'tax_query' => [[
'taxonomy' => 'chef',
'field' => 'term_id',
'terms' => array_column($terms, 'term_id'),
'operator' => 'IN',
]],
]);
// loop and output posts
while ($query->have_posts()) {
$query->the_post();
var_dump(get_the_title());
}
On my product detail page I'm trying to allow users to add multiple cross-sell products to the cart at once.
I found a few examples how this could be done. But most of them work with URL parameters.
For my case I'm trying to do this with ajax. Like the default add-to-cart button.
On my research I found an example here.
But I'm not sure how to adapt it for my case.
At the moment I have a list of product ID's from a custom loop.
Here's my custom loop code:
add_action('woocommerce_single_product_cols_before', 'show_cross_sell_in_single_product', 1200);
function show_cross_sell_in_single_product(){
$crosssells = get_post_meta( get_the_ID(), '_crosssell_ids',true);
$args = array(
'post_type' => 'product',
'posts_per_page'=> -1,
'post__in' => $crosssells,
'tax_query' => array(
array(
'relation' => 'OR',
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'exclude-from-catalog',
'operator' => 'NOT IN',
),
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'exclude-from-catalog',
'operator' => '=',
),
)
),
);
$products_crosssells = new WP_Query( $args );
$products_crosssells_ids = wp_list_pluck( $products_crosssells->posts, 'ID' );
if( $products_crosssells->have_posts() ) : ?>
<div>
<ul>
<?php while ( $products_crosssells->have_posts() ) : $products_crosssells->the_post(); ?>
<?php wc_get_template_part( 'content', 'product-crosssells' ); ?>
<?php endwhile; ?>
</ul>
Add all to the cart
</div>
<?php endif;
wp_reset_postdata();
}
The product ID's are stored in $products_crosssells_ids.
As I understand the example from the link above, I need to add the product ID's to the custom function:
add_action('wp_ajax_multi_add_to_cart', 'multi_ajax_add_to_cart');
add_action('wp_ajax_nopriv_multi_add_to_cart', 'multi_ajax_add_to_cart');
function multi_ajax_add_to_cart() {
if (isset($_POST['items']) ) {
$item_keys = array();
foreach( $_POST['items'] as $item ) {
if( isset($item['id']) ) {
$item_qty = isset($item['qty']) && $item['qty'] > 1 ? $item['qty'] : 1;
$item_keys[] = WC()->cart->add_to_cart($item['id'], $item_qty);
}
}
echo json_encode($item_keys); // Send back cart item keys
}
die();
}
But I'm not sure how I need to add/adapt this code to my product page.
So A user could click on this link and adds everything to the cart:
Add all to the cart
Ok, this is probably an easy one. But I can't seem to figure it out for some reason.
I have a custom post type called: Beachevents.
There i have a couple of events. I also have a custom taxonomy called: Thema.
When making my beachevent pages (not posts) i created some types of thema's (themes). Like: Strand Spellen (the slug is strand-spellen).
Now I want to make a loop that display's only strand-spellen with thumbnail and all that stuff.
Does anyone know how I go about this?
I tryed some codes like these but do don't do the trick.
$args = array(
'post_type' => 'beachevents',
'posts_per_page'=> -1,
'tax_query' => array(
array(
'taxonomy' => 'strand-spellen',
'field' => 'slug',
'terms' => 'all'
)
)
);
$products = new WP_Query( $args );
if( $products->have_posts() ) {
while( $products->have_posts() ) {
$products->the_post();
?>
<div class='content'>
<h2><?php the_title(); ?></h2>
</div>
<?php
}
}
else {
echo 'There seems to be a problem, please try searching again or contact customer support!';
}
Thanks!
You're close!
In your tax_query, taxonomy needs to refer to 'beachevents' and terms needs to refer to 'strand-spellen'.
So, your code will look like this:
'tax_query' => array(
array(
'taxonomy' => 'thema',
'field' => 'slug',
'terms' => 'strand-spellen'
)
)
For more information on building your queries, you may find the WP_Query documentation useful - there's a section in there on taxonomy queries.
Thanks to Tim for his help. Here is my full code for people who encounter this same problem.
<?php $args = array(
'post_type' => 'beachevents',
'posts_per_page'=> -1,
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'thema',
'field' => 'slug',
'terms' => 'strand-spellen'
)
)
);
$products = new WP_Query( $args );
if( $products->have_posts() ) {
while( $products->have_posts() ) {
$products->the_post();
?>
<div class='content'>
<h2><?php the_title(); ?></h2>
</div>
<?php
}
}
else {
echo 'There seems to be a problem, please try searching again or contact customer support!';
} ?>
Including ordered by title and ASC. Hope I coded it correctly...
I'm going to have a WP_Query run with arguments based on user input. The user can select multiple categories/terms and the query will filter based on the AND boolean.
// main arguments
$args = array(
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'industry',
'terms' => $user_input,
),
array(
'taxonomy' => 'format',
'terms' => $user_input2,
),
),
);
// less specific arguments
$lessargs = array(
'tax_query' => array(
array(
'taxonomy' => 'industry',
'terms' => $user_input,
),
),
);
If no results are returned in the first query, I want to run the second query with less specificity ($lessargs). I know I need to use if/else statements but I don't know the correct way to do this within the loop. Example:
<?php $the_query = new WP_Query( $args ); ?>
<?php if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : the_post(); ?>
// Return Query Results
<?php endwhile; ?>
<?php else : ?>
<?php $the_second_query = new WP_Query( $less_args ); ?>
<?php while ($the_second_query->have_posts()) : the_post(); ?>
// Return Second Query Results
<?php endwhile; ?>
<?php endif; ?>
Is this the proper way to conditionally call queries if the previous query returns empty?
I always build my own loop whenever I needed a custom query.
You can probably do it like this,
# First Argument
$args = array(
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'industry',
'terms' => $user_input,
),
array(
'taxonomy' => 'format',
'terms' => $user_input2,
),
),
);
# Second Argument
$lessargs = array(
'tax_query' => array(
array(
'taxonomy' => 'industry',
'terms' => $user_input,
),
),
);
$query = new WP_QUery( $args ); //Run First Query
$posts = $query->get_posts(); //Get Post of first Query
if ( !$posts ) { //If no post on First query Run Second Query
$query = new WP_QUery( $lessargs );
$posts = $query->get_posts(); //Get Post of second query
}
if ( !$posts ) return 'No Result Found'; //stop execution if no results
foreach( $posts as $post ) { //Loop through each result
_e( $post->post_title ); // Echo the title
}
}
I would do it slightly differently, just to cater for the fact that neither query will return any rows:
$the_query = new wp_query($args);
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
the_post();
// Return Query Results
}
} else {
$the_query = new wp_query($lessargs);
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
the_post();
// Return Second Query Results
}
} else {
echo '<p>No posts found.</p>';
}
}
Note you have a typo with your $lessargs variable.