WP_Query and Woocommerce - woocommerce

I have a function inside (*Wordpress Child Theme) functions.php which returns the attached WooCommerce product categories, using global $wp_query. The get_posts() function will only return the number of products for the first page of products. (*the post_per_page value - in this case 16).
I have tried to temporarily set the post_per_page to -1, by adding the code below right before my function call in archive-product.php template:
$wp_query->set('posts_per_page', 999);
$wp_query->query($wp_query->query_vars);
and then resetting the value after the function call inside archive-product.php template
$wp_query->set('posts_per_page', 16);
$wp_query->query($wp_query->query_vars);
This almost works, but messes up the pre_get_posts function (*which sorts the products), and also seems to cause issues with the listing of the product results if over 500 products?
Please any suggestions would be greatly appreciated. Thanks.
//build dynamic category select menu based on attached categories
function dynamic_category_select() {
global $wp_query;
$my_posts = $wp_query->get_posts();
$my_post_ids = wp_list_pluck($my_posts, 'ID');
$categories = wp_get_object_terms($my_post_ids, 'product_cat');
foreach($categories as $category) {
$options[] = '<option value="' . $category->slug . '">' . $category->name . '</option>';
}
$x = '<select id="category-options" name="category-options">';
$x .= '<option value="">Select Category Options</option>';
foreach($options as $option) {
$x .= $option;
}
$x .= '</select>';
return $x;
}

Try replacing your $my_posts = $wp_query->get_posts(); with the following:
// Get current query vars
$my_query_args = $wp_query->query_vars;
// Disable paging - return all results
$my_query_args['nopaging'] = true;
// Create a new query with the modified query vars
$my_query = new WP_Query($my_query_args);
$my_posts = $my_query->get_posts();

Related

Get the terms of a custom taxonomy without explicit taxonomy IDs in Wordpress

I used the following code to try it but it doesnt work. I need all terms of the taxnonmy which are saved for this explicit post. But I only get "array".
get_the_terms( $post->ID, 'regisseur', 'Regisseur: <span class="flight">
Additional I want to except the ID 43 and 76 – this terms shouldnt be shown in the list.
Why it doesnt work?
If you need to get all the terms in a post, you should use wp_get_post_terms() instead of get_the_terms()
Try this (updated)
<?php
$terms = wp_get_post_terms($post->ID, 'regisseur');
if ($terms)
{
$output = '';
foreach ($terms as $term) {
//Here we exclude terms
if ($term->term_id == 43) continue;
if ($term->term_id == 76) continue;
$output .= '<span>' . $term->name . '</span>';
}
};
?>

Display custom Field in loop - Woocommerce category

I have nested categories in my website.
I created a custom Field in Woocommerce category and I tried to Add it in category loop. It shows only the term value of the current category page
add_action( 'woocommerce_after_subcategory_title', 'custom_add_cat_Min_price', 12);
function custom_add_cat_Min_price ($category) {
$prix_min_catt = get_term_meta(get_queried_object_id(), 'prix_min_cat', true);
$terms = get_the_terms( $post->ID, 'prix_min_cat' );
foreach ($terms as $term){
echo '<div class="prixminofcatg">'.$prix_min_catt.'</div>';
}
}
I think the problem is the scope of your function. You've passed the $category to your function, but haven't used it. This gives you the ID of your category:
function custom_add_cat_Min_price ($category) {
$category_id = $category->term_id;
and from there, you should be able to extract the custom fields.
Thanks its working without Foreach
add_action( 'woocommerce_after_subcategory_title', 'custom_addd_cat_Min_price', 29);
function custom_addd_cat_Min_price ($category) {
$category_id = $category->term_id;
$prix_min_cag = get_term_meta($category_id, 'prix_min_cat', true);
$terms = get_term( $category_id, 'prix_min_cat' );
echo '<div class="prixminofcatg">'.$prix_min_cag.'</div>';
}

how to list a repeater sub fields with label and value in Advanced Custom Field?

I've searched and I could not find any solution to list a repeater field rows with Label of sub field and its value.
in my case I want to list a repeater field sub fields with Label and value.
for example :
'Sub Field Label' = 'Value'
is there any way to do this ?
If you know the labels you want to retrieve from your Repeater Field, just use the standard method:
if( have_rows('repeater_field_name') ):
while ( have_rows('repeater_field_name') ) : the_row();
echo 'Label = ' . get_sub_field('sub_field_name') . '<br>';
endwhile;
endif;
If you aren't in a single post/page or outside The Loop, just add the $post_id as the second parameter to your ACF function calls. For example: have_rows('repeater_field_name', $post_id).
If you don't know the label names, I guess you could use get_fields() to get an array of all custom fields for the current post and iterate it. Something like:
$fields = get_fields($post->ID);
foreach ($fields as $field_type => $field) {
if ( $field_type == 'repeater_field' ) {
foreach ($field as $row) {
foreach ($row as $label => $value) {
// In this case you should be aware that
// $value could be an Array too...
echo $label . ' = ' . $value;
}
}
}
}
Anyway, I recommend you to take a look at ACF Documentation. It's complete, clear and with lots of code snippets covering the most common uses.
<?php $args = array('post_type' => 'post');
$the_query = new WP_Query( $args );
query_posts( $args );
while ( have_posts() ) : the_post();
$field = get_field_object('field_name');
echo $field['label']; //print label name
echo the_field('field_name'); //and its value
endwhile; wp_reset_query(); ?>
please try this hope help to you

Category posts as submenu items

Been searching for a while on this subject without any success. All I can find are solutions which involve using the Walker property of the wp_nav_menu, which I don't think is the right approach. I want to create a menu with the following structure:
Link that is a category
X numbers of submenu items which are posts belonging to the parent
category
And so on...
Do I need to manually run a loop for each menu item to retrieve the posts?
Try this in your functions.php:
add_filter('wp_nav_menu_items', 'add_productions', 10, 2);
function add_productions($items, $args) {
$cat = '28'; // define category
$productions = array();
$productions = get_posts("cat=$cat");
if ($productions[0] != '') {
$items .= '<li>Productions<ul class="sub-menu">';
foreach ( $productions as $production ) {
$permalink = get_permalink( $production->ID );
$items .= '<li>'.$production->post_title.'</li>';
}
$items .= '</ul>';
}
return $items;
}
This question and answer was very helpful to me, so thought I would add an additional solution that provides an option to insert the menu at any point in the existing core menu.
Most of the examples on the web show how to add an item to the end of a menu, this will help you add the new menu anywhere in the menu.
Here is the code to place the new menu after my "Home" menu:
add_filter('wp_nav_menu_items', 'add_gallery', 10, 2);
function add_gallery($items, $args) {
$cat = '1'; // define category
$pattern = 'Home</a></li>';
$productions = array();
$productions = get_posts("cat=$cat");
$temp_items = '';
if ($productions[0] != '') {
$temp_items .= '<li>Galleries<ul class="sub-menu">';
foreach ( $productions as $production ) {
$permalink = get_permalink( $production->ID );
$temp_items .= '<li>'.$production->post_title.'</li>';
}
$temp_items .= '</ul>';
}
$replacement = $pattern .$temp_items;
$temp_menu = str_replace($pattern, $replacement, $items);
return $temp_menu;
}
Hopefully this helps someone else...

How to apply different style to category navigation links in wordpress

I am using wordpress 3.0.
Is there any ways to apply different style class to category navigation.
Eg: Consider we have three categories Audio,Video,Upload.Here I need to show Upload category in different style except other two.
Thanks...
You could do it by adding a filter in your theme's functions.php:
function your_list_categories($categories){
$categories = preg_replace('Upload', '<span class="upload">Upload</span>', $categories);
return $categories;
}
add_filter('wp_list_categories', 'your_list_categories');
If you have to do more complex processing, you could use get_categories() method and then loop through and build the output yourself:
function your_list_categories($categories){
$categories= get_categories();
$output = '';
foreach ($categories as $category) {
if($category->name == "Upload"){
$output .= 'Category link code for Upload';
} else {
$output .= 'Category link code for all other category links';
}
}
return $output;
}
add_filter('wp_list_categories', 'your_list_categories');

Resources