Alphabetize Wordpress taxonomy list by last name - wordpress

I have a query which outputs a list of taxonomy tags, organised by FIRST NAME with an alphabetical heading, like so:
E
Elena Ferrante 
H
Helen DeWitt
J
Joan Didion
Jonathan Franzen 
K
Kazuo Ishiguro
M
Maggie Nelson
P
Plato
R
Rachel Cusk
Roberto Bolaño
S
Sheila Heti
etc.
How can I alphabetize this list of taxonomy tags by LAST NAME instead of FIRST NAME?
So the result would be:
B
Roberto Bolaño
C
Rachel Cusk
D
Helen DeWitt
Joan Didion
F
Elena Ferrante
Jonathan Franzen 
H
Sheila Heti 
I
Kazuo Ishiguro
P
Plato
N
Maggie Nelson
etc.
Here is the query:
<?php
$list = '';
$groups = array();
$tags = get_terms('authors',$args);
if( $tags && is_array( $tags ) ) {
foreach( $tags as $tag ) {
$first_letter = strtoupper( $tag->name[0] );
$groups[ $first_letter ][] = $tag;
}
if( !empty( $groups ) ) {
foreach( $groups as $letter => $tags ) {
$list .= "<div class='titleLetter'>" . $letter . "</div><ul>";
foreach( $tags as $tag ) {
$list .= '<li>'.$tag->name.'</li>';
}
$list .= '<br></ul>';
}
$list .= '';
}
}
echo $list; ?>

If the given code works for First Name ordering. Then this one will work for orderby lastname.
<?php
$list = '';
$groups = array();
$tags = get_terms('authors',$args);
if( $tags && is_array( $tags ) ) {
$i=0;
foreach( $tags as $tag ) {
$lastname_arr=explode(' ', $tag->name);
$lastname=$lastname_arr[count($lastname_arr)-1];
$first_letter = strtoupper( $lastname[0] );
$groups[ $first_letter ][$lastname.'_'.$i] = $tag;
$i++;
}
foreach($groups as $key=>$group ){
ksort($groups[$key]);
}
ksort($groups);
if( !empty( $groups ) ) {
foreach( $groups as $letter => $tags ) {
$list .= "<div class='titleLetter'>" . $letter . "</div><ul>";
foreach( $tags as $tag ) {
$list .= '<li>'.$tag->name.'</li>';
}
$list .= '<br></ul>';
}
$list .= '';
}
}
echo $list;
?>
What i did here are:
replaced array index with the first letters of lastnames.
applied ksort(sort by key) function to array and sub-arrays (in order to create custom orderby on new indexes)

Related

Update Parent Product Quantity When Variation Quantity Changes

I am actually trying to update the stock quantity of parent products whenever the quantity of any of its variation increases or decreases.
What could be the error in following code.
function wc_get_variable_product_stock_quantity( $output = 'raw', $product_id = 0 ){
global $wpdb, $product;
// Get the product ID (can be defined)
$product_id = $product_id > 0 ? $product_id : get_the_id();
// Check and get the instance of the WC_Product Object
$product = is_a( $product, 'WC_Product' ) ? $product : wc_get_product($product_id);
// Only for variable product type
if( $product->is_type('variable') ){
// Get the stock quantity sum of all product variations (children)
$stock_quantity = $wpdb->get_var("
SELECT SUM(pm.meta_value)
FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type = 'product_variation'
AND p.post_status = 'publish'
AND p.post_parent = '$product_id'
AND pm.meta_key = '_stock'
AND pm.meta_value IS NOT NULL
");
// Preparing formatted output
if ( $stock_quantity > 0 ) {
$html = '<p class="stock in-stock">'. sprintf( __("%s in stock", "woocommerce"), $stock_quantity ).'</p>';
} else {
if ( is_numeric($stock_quantity) )
$html = '<p class="stock out-of-stock">' . __("Out of stock", "woocommerce") . '</p>';
else
$html = '';
}
// Different output options
if( $output == 'echo_html' )
echo $html;
elseif( $output == 'return_html' )
return $html;
else
return $stock_quantity;
}
}

Update Product Stock When Variation Stock Changes in WooCommerce

I am trying the update total stock of the product when stock for any variation changes. I want to keep the total stock of all the variations updated.
Here is the code I am using but it's not working. What could be the reason.
add_action( 'woocommerce_product_set_stock', 'wc_get_variable_product_stock_quantity' );
function wc_get_variable_product_stock_quantity( $output = 'raw', $product_id = 0 ){
global $wpdb, $product;
// Get the product ID (can be defined)
$product_id = $product_id > 0 ? $product_id : get_the_id();
// Check and get the instance of the WC_Product Object
$product = is_a( $product, 'WC_Product' ) ? $product : wc_get_product($product_id);
// Only for variable product type
if( $product->is_type('variable') ){
// Get the stock quantity sum of all product variations (children)
$stock_quantity = $wpdb->get_var("
SELECT SUM(pm.meta_value)
FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type = 'product_variation'
AND p.post_status = 'publish'
AND p.post_parent = '$product_id'
AND pm.meta_key = '_stock'
AND pm.meta_value IS NOT NULL
");
// Preparing formatted output
if ( $stock_quantity > 0 ) {
$html = '<p class="stock in-stock">'. sprintf( __("%s in stock", "woocommerce"), $stock_quantity ).'</p>';
} else {
if ( is_numeric($stock_quantity) )
$html = '<p class="stock out-of-stock">' . __("Out of stock", "woocommerce") . '</p>';
else
$html = '';
}
// Different output options
if( $output == 'echo_html' )
echo $html;
elseif( $output == 'return_html' )
return $html;
else
return $stock_quantity;
set_stock_quantity( $stock_quantity );
}
}

woocommerce variable product price on dropdown

Hello I m trying to make available the prices for the variations on the dropdown, at some point I were able to achieve it when were only one variable, but now I have two variables for the product but the price on the dropdown is not updating.
Based on Variable product attribute: Customizing each displayed radio buttons text value answer code, here is the code I use to get on the first step, but I need help to update the price of variations up to the second variable selected.
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
if ( empty( $term ) ) return $term;
if ( empty( $product->id ) ) return $term;
$id = $product->get_id();
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
$term_slug = ( !empty( $result ) ) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_key != 'attribute_pa_alto'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = $id";
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );
//EDITADO PARA MOSTRAR CUANDO NO HAY STOCK MUESTRA AGOTADO
if($_product->managing_stock() && $_product->get_stock_quantity() < 1){
return $term . ' - AGOTADO';
}else{
return $term . ' - (' . wp_kses( woocommerce_price( $_product->get_price() ), array() ) . ')';
}
}
return $term;
}

Retrieve Wordpress 3.5 gallery images NOT attached

I am trying to find out how to use this code referencing the answer to the question here:
`global $post;
$post_subtitrare = get_post( $post->ID );
$content = $post_subtitrare->post_content;
$pattern = get_shortcode_regex();
preg_match( "/$pattern/s", $content, $match );
if( isset( $match[2] ) && ( "gallery" == $match[2] ) ) {
$atts = shortcode_parse_atts( $match[3] );
$attachments = isset( $atts['ids'] ) ? explode( ',', $atts['ids'] ) : get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID .'&order=ASC&orderby=menu_order ID' );
}`
to retrieve the same data as the get_children()? Currently, only ids are retrieved but I have tried get_post() and get_posts().
Serialized data from the get_children array:
{i:229;O:7:"WP_Post":24:{s:2:"ID";i:229;s:11:"post_author";s:1:"1";
s:9:"post_date";s:19:"2012-12-27 21:01:49";s:13:"post_date_gmt";
s:19:"2012-12-27 21:01:49";s:12:"post_content";s:0:""
;s:10:"post_title";s:8:"DSCN0703";s:12:"post_excerpt";
s:0:"";s:11:"post_status";s:7:"inherit";
s:14:"comment_status";s:6:"closed";
s:11:"ping_status";s:4:"open";s:13:"post_password";s:0:"";
s:9:"post_name";s:10:"dscn0703-6";s:7:"to_ping";s:0:"";s:6:"pinged";s:0:"";
s:13:"post_modified";s:19:"2012-12-27 21:01:49";s:17:"post_modified_gmt";
s:19:"2012-12-27 21:01:49";s:21:"post_content_filtered";s:0:"";
s:11:"post_parent";i:223;s:4:"guid";s:81:"http:/exampleurl.com/wp-content/uploads/2012/12/DSCN07031.jpg";s:10:"menu_order";i:1;s:9:"post_type";s:10:"attachment";s:14:"post_mime_type";s:10:"image/jpeg";s:13:"comment_count";s:1:"0";s:6:"filter";s:3:"raw";}
Serialized data from the code:
a:7:{i:0;s:3:"229";i:1;s:3:"225";i:2;s:3:"228";i:3;s:3:"230";i:4;s:3:"226";i:5;s:3:"227";i:6;s:3:"232";}
Can someone point me to the wp hook that will give me the same data as the get_children from the ids generated from the above code?
Not 100% sure what you're after but this is a copy of the code I used ( based on that answer with some additions ) ... it may help
$post_subtitrare = get_post( $post->ID );
$content = $post_subtitrare->post_content;
$pattern = get_shortcode_regex();
preg_match( "/$pattern/s", $content, $match );
if ( isset( $match[2] ) && ( "gallery" == $match[2] ) ) {
$atts = shortcode_parse_atts( $match[3] );
$images = isset( $atts['ids'] ) ? explode( ',', $atts['ids'] ) : get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID .'&order=ASC&orderby=menu_order ID' );
}
if ( $images ) :
$first_element = array_shift(array_values($images));
if ( ! is_object($first_element)) {
$image_count = count( $images );
$first_image = $images[0];
$featured = wp_get_attachment_image( $first_image, 'blog-featured-image' );
}
else {
$image_count = count( $images );
$first_image = array_shift(array_values($images));
$imageID = $first_image->ID;
$featured = wp_get_attachment_image( $imageID, 'blog-featured-image' );
}
... this gave me some variables which I could use including $featured.

Break Up WordPress Archive List into Multiple Columns

I want to break up this archive list into multiple columns (three columns)...I don't want to use CSS3 for this. I can easily do this with wp_list_categories, but that method doesn't work with wp_get_archives.
Ideally, I'd like to implement a counter to add in tags at an approximate 1/3 count...then I can use CSS for the rest.
CUSTOM ARCHIVE FUNCTION:
function wp_custom_archive($args = '') {
global $wpdb, $wp_locale;
$defaults = array(
'limit' => '',
'format' => 'html', 'before' => '',
'after' => '', 'show_post_count' => false,
'echo' => 1
);
$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
if ( '' != $limit ) {
$limit = absint($limit);
$limit = ' LIMIT '.$limit;
}
// over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
$archive_date_format_over_ride = 0;
// options for daily archive (only if you over-ride the general date format)
$archive_day_date_format = 'Y/m/d';
// options for weekly archive (only if you over-ride the general date format)
$archive_week_start_date_format = 'Y/m/d';
$archive_week_end_date_format = 'Y/m/d';
if ( !$archive_date_format_over_ride ) {
$archive_day_date_format = get_option('date_format');
$archive_week_start_date_format = get_option('date_format');
$archive_week_end_date_format = get_option('date_format');
}
//filters
$where = apply_filters('customarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
$join = apply_filters('customarchives_join', "", $r);
$output = '<ul>';
$query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit";
$key = md5($query);
$cache = wp_cache_get( 'wp_custom_archive' , 'general');
if ( !isset( $cache[ $key ] ) ) {
$arcresults = $wpdb->get_results($query);
$cache[ $key ] = $arcresults;
wp_cache_set( 'wp_custom_archive', $cache, 'general' );
} else {
$arcresults = $cache[ $key ];
}
if ( $arcresults ) {
$afterafter = $after;
foreach ( (array) $arcresults as $arcresult ) {
$url = get_month_link( $arcresult->year, $arcresult->month );
/* translators: 1: month name, 2: 4-digit year */
$text = sprintf(__('%s'), $wp_locale->get_month($arcresult->month));
$year_text = sprintf('<li>%d</li>', $arcresult->year);
if ( $show_post_count )
$after = ' ('.$arcresult->posts.')' . $afterafter;
$output .= ( $arcresult->year != $temp_year ) ? $year_text : '';
$output .= get_archives_link($url, $text, $format, $before, $after);
$temp_year = $arcresult->year;
}
}
$output .= '</ul>';
if ( $echo )
echo $output;
else
return $output;
}
CURRENT HTML OUTPUT (shortened):
<ul>
<li>2012</li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/09/' title='September'>September</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/08/' title='August'>August</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/07/' title='July'>July</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/06/' title='June'>June</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/05/' title='May'>May</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/04/' title='April'>April</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/03/' title='March'>March</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/02/' title='February'>February</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/01/' title='January'>January</a></li>
</ul>
If I understand correctly, you would have to do something like this
$output = '<ul class="col1">';
if ( $arcresults ) {
$numarticles = count($arcresults);
$column1 = round($numarticles/3);
$column2 = round($column1*2);
$counter = 0;
$afterafter = $after;
foreach ( (array) $arcresults as $arcresult ) {
if($counter == $column1) {
$output .= "</ul><ul class='col2'>";
} elseif($counter == $column2) {
$output .= "</ul><ul class='col3'>";
}
$url = get_month_link( $arcresult->year, $arcresult->month );
/* translators: 1: month name, 2: 4-digit year */
$text = sprintf(__('%s'), $wp_locale->get_month($arcresult->month));
$year_text = sprintf('<li>%d</li>', $arcresult->year);
if ( $show_post_count )
$after = ' ('.$arcresult->posts.')' . $afterafter;
$output .= ( $arcresult->year != $temp_year ) ? $year_text : '';
$output .= get_archives_link($url, $text, $format, $before, $after);
$temp_year = $arcresult->year;
$counter ++;
}
$output .= '</ul>';
}
This way, when the counter gets to a third of the way, it will add another list, so at the end you will have 3 lists.
Hope this helps.

Resources