WP_Query with previous_posts_link() and next_posts_link( - wordpress

I'm working on an archive page for my website. I've created a page with an archive template and I'm using WP_Query to get the posts that match certain variables. I use the following code:
$myposts = new WP_Query(array('author'=>$writer,'m'=>$date, 'cat'=>$category, 'posts_per_page'=>5)); ?>
and
<?php if($myposts->have_posts()){
while ( $myposts->have_posts() ) {
$myposts->the_post();
include 'article-box.php';
}
} ?>
where 'articles.php generates the posts. Then comes the part which doesn't work.
<div id="pages-nav">
<div class="alignleft"><?php previous_posts_link('« Previous Articles') ?></div>
<div class="alignright"><?php next_posts_link('Next Articles »','') ?></div>
</div>
The links for the next / previous pages don't show up at all. Is this to do with the fact that I use a page with archive template or is there anything else I'm missing?
If I enter
[http://localhost/wordpress/archives/page/2/?date=201307&category=5&writer=0&submit=search]
as URL I get the next posts as it should be with the "Previous" link but not the "Next" link
Edit:
print_r($myposts) returns:
WP_Query Object ( [query_vars] => Array ( [author] => 0 [m] => 201307 [cat] => 7 [posts_per_page] => 5 [paged] => 1 [error] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => carolynne [tag] => [tag_id] => [author_name] => [feed] => [tb] => [comments_popup] => [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [fields] => [menu_order] => [category__in] => Array ( [0] => 7 [1] => 10 [2] => 11 [3] => 12 [4] => 54 ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1 [update_post_term_cache] => 1 [update_post_meta_cache] => 1 [post_type] => [nopaging] => [comments_per_page] => 50 [no_found_rows] => [order] => DESC ) [tax_query] => WP_Tax_Query Object ( [queries] => Array ( [0] => Array ( [taxonomy] => category [terms] => Array ( [0] => 7 [1] => 10 [2] => 11 [3] => 12 [4] => 54 ) [include_children] => [field] => term_id [operator] => IN ) ) [relation] => AND ) [meta_query] => WP_Meta_Query Object ( [queries] => Array ( ) [relation] => ) [post_count] => 5 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 22 [max_num_pages] => 5 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => 1 [is_date] => 1 [is_year] => [is_month] => 1 [is_day] => [is_time] => [is_author] => [is_category] => 1 [is_tag] => [is_tax] => [is_search] => [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => [is_404] => [is_comments_popup] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => [query_vars_hash] => 5d9c9d82fe6cd2bec72ca33ab10482e7 [query_vars_changed] => [thumbnails_cached] => [query] => Array ( [author] => 0 [m] => 201307 [cat] => 7 [posts_per_page] => 5 [paged] => 1 ) [request] => SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND YEAR(wp_posts.post_date)=2013 AND MONTH(wp_posts.post_date)=07 AND ( wp_term_relationships.term_taxonomy_id IN (7,10,11,12,56) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 5 [posts] => Array ( [0] => WP_Post Object ( [ID] => 43393 [post_author] => 1013 [post_date] => 2013-07-31 18:17:44 [post_date_gmt] => 2013-07-31 17:17:44 [post_content] => This film is a cross [...]

Send $paged variable to query as like below:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$myposts = new WP_Query(array('author'=>$writer,'m'=>$date, 'cat'=>$category, 'posts_per_page'=>5,'paged' => $paged));
And change your pagination code to below:
<div id="pages-nav">
<div class="alignleft">
<?php previous_posts_link('« Previous Articles',$myposts->max_num_pages); ?>
</div>
<div class="alignright">
<?php next_posts_link('Next Articles »',$myposts->max_num_pages); ?>
</div>
</div>

Related

How can I add a filter for specific categories on functions.php?

I am on functions.php and I want to add this filter:
function graphene_alter_the_query( $request ) {
$dummy_query = new WP_Query();
$dummy_query->parse_query( $request );
if (!is_category('25')) {
$request['gdsr_sort'] = 'rating';
}
else {
$request['gdsr_sort'] = 'comment_count';
}
return $request;
}
add_filter( 'request', 'graphene_alter_the_query' );
the filter should do the same thing for all categories except cat 25 but seems like it cant tell it apart like if the IF condition is not detecting the category so I tried to debug it by looking at the values in WP_Query and I get this:
$myquery = new WP_Query(); print_r($myquery);
WP_Query Object ( [query] => Array ( [category_name] => 'my cat' ) [query_vars] => Array ( [category_name] => 'my cat' [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [tag] => [cat] => [tag_id] => [author] => [author_name] => [feed] => [tb] => [paged] => 0 [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [title] => [fields] => [menu_order] => [embed] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [post_name__in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [post_parent__in] => Array ( ) [post_parent__not_in] => Array ( ) [author__in] => Array ( ) [author__not_in] => Array ( ) ) [tax_query] => WP_Tax_Query Object ( [queries] => Array ( [0] => Array ( [taxonomy] => category [terms] => Array ( [0] => 'my cat' ) [field] => slug [operator] => IN [include_children] => 1 ) ) [relation] => AND [table_aliases:protected] => Array ( ) [queried_terms] => Array ( [category] => Array ( [terms] => Array ( [0] => 'my cat' ) [field] => slug ) ) [primary_table] => [primary_id_column] => ) [meta_query] => [date_query] => [post_count] => 0 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 0 [max_num_pages] => 0 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => 1 [is_date] => [is_year] => [is_month] => [is_day] => [is_time] => [is_author] => [is_category] => 1 [is_tag] => [is_tax] => [is_search] => [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => [is_privacy_policy] => [is_404] => [is_embed] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_favicon] => [is_posts_page] => [is_post_type_archive] => [query_vars_hash:WP_Query:private] => 6d85cdf18f3e6a58a470a3002d5807fc [query_vars_changed:WP_Query:private] => [thumbnails_cached] => [stopwords:WP_Query:private] => [compat_fields:WP_Query:private] => Array ( [0] => query_vars_hash [1] => query_vars_changed ) [compat_methods:WP_Query:private] => Array ( [0] => init_query_flags [1] =>
but both tag_id and cat appear empty. Why is that?

WooCommerce get product name, price etc from Object

I have this code:
$args = array(
'category' => array( 'membership-level' ),
'orderby' => 'name',
);
$products = wc_get_products( $args );
echo '<pre>';
print_r( $products );
echo '</pre>';
And its output is something like that:
Array
(
[0] => WC_Product_Simple Object
(
[object_type:protected] => product
[post_type:protected] => product
[cache_group:protected] => products
[data:protected] => Array
(
[name] => Test Level
[slug] => test-level
[date_created] => WC_DateTime Object
(
[utc_offset:protected] => 0
[date] => 2022-04-10 11:54:18.000000
[timezone_type] => 1
[timezone] => +00:00
)
[date_modified] => WC_DateTime Object
(
[utc_offset:protected] => 0
[date] => 2022-04-10 11:54:18.000000
[timezone_type] => 1
[timezone] => +00:00
)
[status] => publish
[featured] =>
[catalog_visibility] => visible
[description] => Hello Test Level
[short_description] =>
[sku] =>
[price] => 12
[regular_price] => 12
[sale_price] =>
[date_on_sale_from] =>
[date_on_sale_to] =>
[total_sales] => 1
[tax_status] => taxable
[tax_class] =>
[manage_stock] =>
[stock_quantity] =>
[stock_status] => instock
[backorders] => no
[low_stock_amount] =>
[sold_individually] =>
[weight] =>
[length] =>
[width] =>
[height] =>
[upsell_ids] => Array
(
)
[cross_sell_ids] => Array
(
)
[parent_id] => 0
[reviews_allowed] => 1
[purchase_note] =>
[attributes] => Array
(
)
[default_attributes] => Array
(
)
[menu_order] => 0
[post_password] =>
[virtual] => 1
[downloadable] =>
[category_ids] => Array
(
[0] => 141
)
[tag_ids] => Array
(
)
[shipping_class_id] => 0
[downloads] => Array
(
)
[image_id] =>
[gallery_image_ids] => Array
(
)
[download_limit] => -1
[download_expiry] => -1
[rating_counts] => Array
(
)
[average_rating] => 0
[review_count] => 0
)
[supports:protected] => Array
(
[0] => ajax_add_to_cart
)
[id:protected] => 5275
[changes:protected] => Array
(
)
[object_read:protected] => 1
[extra_data:protected] => Array
(
)
[default_data:protected] => Array
(
[name] =>
[slug] =>
[date_created] =>
[date_modified] =>
[status] =>
[featured] =>
[catalog_visibility] => visible
[description] =>
[short_description] =>
[sku] =>
[price] =>
[regular_price] =>
[sale_price] =>
[date_on_sale_from] =>
[date_on_sale_to] =>
[total_sales] => 0
[tax_status] => taxable
[tax_class] =>
[manage_stock] =>
[stock_quantity] =>
[stock_status] => instock
[backorders] => no
[low_stock_amount] =>
[sold_individually] =>
[weight] =>
[length] =>
[width] =>
[height] =>
[upsell_ids] => Array
(
)
[cross_sell_ids] => Array
(
)
[parent_id] => 0
[reviews_allowed] => 1
[purchase_note] =>
[attributes] => Array
(
)
[default_attributes] => Array
(
)
[menu_order] => 0
[post_password] =>
[virtual] =>
[downloadable] =>
[category_ids] => Array
(
)
[tag_ids] => Array
(
)
[shipping_class_id] => 0
[downloads] => Array
(
)
[image_id] =>
[gallery_image_ids] => Array
(
)
[download_limit] => -1
[download_expiry] => -1
[rating_counts] => Array
(
)
[average_rating] => 0
[review_count] => 0
)
[data_store:protected] => WC_Data_Store Object
(
[instance:WC_Data_Store:private] => WC_Product_Data_Store_CPT Object
(
[internal_meta_keys:protected] => Array
(
[0] => _visibility
[1] => _sku
[2] => _price
[3] => _regular_price
[4] => _sale_price
[5] => _sale_price_dates_from
[6] => _sale_price_dates_to
[7] => total_sales
[8] => _tax_status
[9] => _tax_class
[10] => _manage_stock
[11] => _stock
[12] => _stock_status
[13] => _backorders
[14] => _low_stock_amount
[15] => _sold_individually
[16] => _weight
[17] => _length
[18] => _width
[19] => _height
[20] => _upsell_ids
[21] => _crosssell_ids
[22] => _purchase_note
[23] => _default_attributes
[24] => _product_attributes
[25] => _virtual
[26] => _downloadable
[27] => _download_limit
[28] => _download_expiry
[29] => _featured
[30] => _downloadable_files
[31] => _wc_rating_count
[32] => _wc_average_rating
[33] => _wc_review_count
[34] => _variation_description
[35] => _thumbnail_id
[36] => _file_paths
[37] => _product_image_gallery
[38] => _product_version
[39] => _wp_old_slug
[40] => _edit_last
[41] => _edit_lock
)
[must_exist_meta_keys:protected] => Array
(
[0] => _tax_class
)
[extra_data_saved:protected] =>
[updated_props:protected] => Array
(
)
[meta_type:protected] => post
[object_id_field_for_meta:protected] =>
)
[stores:WC_Data_Store:private] => Array
(
[coupon] => WC_Coupon_Data_Store_CPT
[customer] => WC_Customer_Data_Store
[customer-download] => WC_Customer_Download_Data_Store
[customer-download-log] => WC_Customer_Download_Log_Data_Store
[customer-session] => WC_Customer_Data_Store_Session
[order] => WC_Order_Data_Store_CPT
[order-refund] => WC_Order_Refund_Data_Store_CPT
[order-item] => WC_Order_Item_Data_Store
[order-item-coupon] => WC_Order_Item_Coupon_Data_Store
[order-item-fee] => WC_Order_Item_Fee_Data_Store
[order-item-product] => WC_Order_Item_Product_Data_Store
[order-item-shipping] => WC_Order_Item_Shipping_Data_Store
[order-item-tax] => WC_Order_Item_Tax_Data_Store
[payment-token] => WC_Payment_Token_Data_Store
[product] => WC_Product_Data_Store_CPT
[product-grouped] => WC_Product_Grouped_Data_Store_CPT
[product-variable] => WC_Product_Variable_Data_Store_CPT
[product-variation] => WC_Product_Variation_Data_Store_CPT
[shipping-zone] => WC_Shipping_Zone_Data_Store
[webhook] => WC_Webhook_Data_Store
[order-item-line_item_removed] => WC_Order_Item_Product_Data_Store
[order-item-line_item_switched] => WC_Order_Item_Product_Data_Store
[order-item-coupon_pending_switch] => WC_Order_Item_Coupon_Data_Store
[order-item-fee_pending_switch] => WC_Order_Item_Fee_Data_Store
[subscription] => WCS_Subscription_Data_Store_CPT
[product-variable-subscription] => WCS_Product_Variable_Data_Store_CPT
[product-subscription_variation] => WC_Product_Variation_Data_Store_CPT
[order-item-line_item_pending_switch] => WC_Order_Item_Product_Data_Store
[report-revenue-stats] => Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore
[report-orders] => Automattic\WooCommerce\Admin\API\Reports\Orders\DataStore
[report-orders-stats] => Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore
[report-products] => Automattic\WooCommerce\Admin\API\Reports\Products\DataStore
[report-variations] => Automattic\WooCommerce\Admin\API\Reports\Variations\DataStore
[report-products-stats] => Automattic\WooCommerce\Admin\API\Reports\Products\Stats\DataStore
[report-variations-stats] => Automattic\WooCommerce\Admin\API\Reports\Variations\Stats\DataStore
[report-categories] => Automattic\WooCommerce\Admin\API\Reports\Categories\DataStore
[report-taxes] => Automattic\WooCommerce\Admin\API\Reports\Taxes\DataStore
[report-taxes-stats] => Automattic\WooCommerce\Admin\API\Reports\Taxes\Stats\DataStore
[report-coupons] => Automattic\WooCommerce\Admin\API\Reports\Coupons\DataStore
[report-coupons-stats] => Automattic\WooCommerce\Admin\API\Reports\Coupons\Stats\DataStore
[report-downloads] => Automattic\WooCommerce\Admin\API\Reports\Downloads\DataStore
[report-downloads-stats] => Automattic\WooCommerce\Admin\API\Reports\Downloads\Stats\DataStore
[admin-note] => Automattic\WooCommerce\Admin\Notes\DataStore
[report-customers] => Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore
[report-customers-stats] => Automattic\WooCommerce\Admin\API\Reports\Customers\Stats\DataStore
[report-stock-stats] => Automattic\WooCommerce\Admin\API\Reports\Stock\Stats\DataStore
)
[current_class_name:WC_Data_Store:private] => WC_Product_Data_Store_CPT
[object_type:WC_Data_Store:private] => product-simple
)
[meta_data:protected] =>
)
Now I want to get the name, and price from that output. If I use $products->get_name(); then I am getting a error message:
Fatal error: Uncaught Error: Call to a member function get_name() on
array in
So, my question is how can I use the WooCommerce methods to get name, price, etc from that $product variable?

How to pass variables to attributes to variations, of a Woocomerce variable product?

I have a function that it is supposed to create a variation and fill the variation with the values, the attributes of the variable product. To become more flexible i didn't create values for the products attributes besides
I have tried a hundred ways the make the values stick to the variation but they dont. The variation is created in the backend but it has none of the values atributed?????? PLEEEEASE HELP!!!! Im at this for 4 days now. ;(
The only values attributed to the product's atributes is ">>EMPTY<<" value. ///////////// Variables $data -> Array (Key -> Value), so that the keys == to the products atributes $product -> $product_id
function add_variable_product_to_cart($product_id, $data) {
$my_post = array(
'post_title'=> 'Order_Printaway',
'post_name' => 'product_' . $data["itemIdentifier"],
'post_status' => 'publish',
'post_parent' => $product_id ,//post is a child post of product post
'post_type' => 'product_variation',//set post type to product_variation
'guid'=>home_url() . '/?product_variation=product_' . $data["itemIdentifier"]
);
//Insert ea. post/variation into database:
$attID = wp_insert_post( $my_post );
update_post_meta($attID, '_price', $data["final_price"]);
update_post_meta($attID, '_regular_price', $data["final_price"]);
foreach($data as $key => $value){
$avail_attributes = array($value);
wp_set_object_terms($product_id, $avail_attributes, $key);
$thedata[$key]['name'] = $value;
}
update_post_meta( $attID,'_product_attributes',$thedata);
if ( ! is_admin() ) {
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
// if product not found, add it
if ( ! $found ){
WC()->cart->add_to_cart( $product_id, 1, $attID);
}
}
else{
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id, 1, $attID);
}
}
}
///////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////// OUTPUT //////////////////////////////////////////////////////////////////////
//Atributes of the Product ->
Array ( [a_color] => Array ( [name] => a_color [value] => >>EMPTY<< [position] => 0 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_creasing] => Array ( [name] => a_creasing [value] => >>EMPTY<< [position] => 1 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_finishing] => Array ( [name] => a_finishing [value] => >>EMPTY<< [position] => 2 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_finishing_desc] => Array ( [name] => a_finishing_desc [value] => >>EMPTY<< [position] => 3 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_format_dir] => Array ( [name] => a_format_dir [value] => >>EMPTY<< [position] => 4 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_page] => Array ( [name] => a_page [value] => >>EMPTY<< [position] => 5 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_paper] => Array ( [name] => a_paper [value] => >>EMPTY<< [position] => 6 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_paper_desc] => Array ( [name] => a_paper_desc [value] => >>EMPTY<< [position] => 7 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_perforation] => Array ( [name] => a_perforation [value] => >>EMPTY<< [position] => 8 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [a_processing] => Array ( [name] => a_processing [value] => >>EMPTY<< [position] => 9 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [product] => Array ( [name] => product [value] => >>EMPTY<< [position] => 10 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) [shipping] => Array ( [name] => shipping [value] => >>EMPTY<< [position] => 11 [is_visible] => 0 [is_variation] => 1 [is_taxonomy] => 0 ) )
////Array of data that i am trying to introduce in the Variation
Array ( [languageCode] => Array ( [name] => en ) [countryCode] => Array ( [name] => GB ) [saveItem] => Array ( [name] => save item by name ) [itemIdentifier] => Array ( [name] => asdasadasds ) [product] => Array ( [name] => flyer ) [a_page] => Array ( [name] => A_1stg ) [a_format] => Array ( [name] => A_105_x_148_mm_DIN-A-6 ) [a_format_dir] => Array ( [name] => A_panel_format ) [a_paper] => Array ( [name] => A_115gqm_Bilderdruck ) [a_paper_desc] => Array ( [name] => A_paper_gloss_no_core ) [a_color] => Array ( [name] => A_4/0-farbig_Euroskala ) [a_finishing] => Array ( [name] => A_Ohne_Finishing ) [a_finishing_desc] => Array ( [name] => A_Ohne_Veredelung ) [a_processing] => Array ( [name] => A_Cutt ) [a_creasing] => Array ( [name] => A_No_Groove ) [a_perforation] => Array ( [name] => A_No_Perfo ) [quantity] => Array ( [name] => 100_quantity ) [shipping] => Array ( [name] => standard ) [priceNet] => Array ( [name] => 8,31 ) [priceGross] => Array ( [name] => 9,97 ) [shippingNet] => Array ( [name] => 4,13 ) [shippingGross] => Array ( [name] => 4,96 ) [final_price] => Array ( [name] => 17.916 ) ) Atributes of the variation after get_post_meta(757, '_product_attributes', true)) Array ( [0] => Array ( [languageCode] => Array ( [name] => en ) [countryCode] => Array ( [name] => GB ) [saveItem] => Array ( [name] => save item by name ) [itemIdentifier] => Array ( [name] => asdasadasds ) [product] => Array ( [name] => flyer ) [a_page] => Array ( [name] => A_1stg ) [a_format] => Array ( [name] => A_105_x_148_mm_DIN-A-6 ) [a_format_dir] => Array ( [name] => A_panel_format ) [a_paper] => Array ( [name] => A_115gqm_Bilderdruck ) [a_paper_desc] => Array ( [name] => A_paper_gloss_no_core ) [a_color] => Array ( [name] => A_4/0-farbig_Euroskala ) [a_finishing] => Array ( [name] => A_Ohne_Finishing ) [a_finishing_desc] => Array ( [name] => A_Ohne_Veredelung ) [a_processing] => Array ( [name] => A_Cutt ) [a_creasing] => Array ( [name] => A_No_Groove ) [a_perforation] => Array ( [name] => A_No_Perfo ) [quantity] => Array ( [name] => 100_quantity ) [shipping] => Array ( [name] => standard ) [priceNet] => Array ( [name] => 8,31 ) [priceGross] => Array ( [name] => 9,97 ) [shippingNet] => Array ( [name] => 4,13 ) [shippingGross] => Array ( [name] => 4,96 ) [final_price] => Array ( [name] => 17.916 ) ) )
I struggled with same issue and the key was in attributes' taxonomies.
If attribute marked as taxonomy then it requires slug instead of string for text based attrs. So firstly check
if ( $attribute['is_taxonomy'] ) {
$options = wc_get_product_terms( $post_id, $attribute['name'], array( 'fields' => 'slugs' ) );
} else {
$options = explode( WC_DELIMITER, $attribute['value'] );
}
and fill $attr_val with slug or string value.
update_post_meta( $post_id, 'attribute_' . sanitize_title($attribute['name'] ), $attr_val);
WC_AJAX::save_attributes() may be helpful

WP_Query returns nothing

I'm trying to make a function
that adds a shortcode which replace the shortcode into the list of attached files with pagination.
The problem is, WP_Query() doesn't return anything.
The functions's code is below.
It's in my functions.php, and It will be called in my content-page.php.
$a = shortcode_atts( array( 'number' => '10'), $atts );
//Protect against arbitrary paged values
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'post_type' => 'attachment',
'post_parent' => get_the_ID(),
'posts_per_page' => $a['number'],
'paged' => $paged
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
$big = 999999999; // need an unlikely integer
$retuen_string .= paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
wp_reset_postdata();
else :
_e( 'Sorry, no posts matched your criteria.' );
endif;
And print_r($the_query) result is below.
WP_Query Object (
[query_vars] => Array (
[post_type] => attachment
[post_parent] => 26
[posts_per_page] => 15
[paged] => 1
[error] =>
[m] =>
[p] => 0
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[comments_popup] =>
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[fields] =>
[menu_order] =>
[category__in] => Array ( )
[category__not_in] => Array ( )
[category__and] => Array ( )
[post__in] => Array ( )
[post__not_in] => Array ( )
[tag__in] => Array ( )
[tag__not_in] => Array ( )
[tag__and] => Array ( )
[tag_slug__in] => Array ( )
[tag_slug__and] => Array ( )
[post_parent__in] => Array ( )
[post_parent__not_in] => Array ( )
[author__in] => Array ( )
[author__not_in] => Array ( )
[ignore_sticky_posts] =>
[suppress_filters] =>
[cache_results] => 1
[update_post_term_cache] => 1
[update_post_meta_cache] => 1
[nopaging] =>
[comments_per_page] => 50
[no_found_rows] =>
[order] => DESC
)
[tax_query] => WP_Tax_Query Object (
[queries] => Array ( )
[relation] => AND
)
[meta_query] => WP_Meta_Query Object (
[queries] => Array ( )
[relation] =>
)
[date_query] =>
[post_count] => 0
[current_post] => -1
[in_the_loop] =>
[comment_count] => 0
[current_comment] => -1
[found_posts] => 0
[max_num_pages] => 0
[max_num_comment_pages] => 0
[is_single] => [is_preview] =>
[is_page] =>
[is_archive] =>
[is_date] =>
[is_year] =>
[is_month] =>
[is_day] =>
[is_time] =>
[is_author] =>
[is_category] =>
[is_tag] =>
[is_tax] =>
[is_search] =>
[is_feed] =>
[is_comment_feed] =>
[is_trackback] =>
[is_home] => 1
[is_404] =>
[is_comments_popup] =>
[is_paged] =>
[is_admin] =>
[is_attachment] =>
[is_singular] =>
[is_robots] =>
[is_posts_page] =>
[is_post_type_archive] =>
[query_vars_hash] => ab31e2fb8fd323e014706374fb98b349
[query_vars_changed] =>
[thumbnails_cached] =>
[stopwords:WP_Query:private] =>
[query] => Array (
[post_type] => attachment
[post_parent] => 26
[posts_per_page] => 15
[paged] => 1
)
[request] => SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_parent = 26 AND wp_posts.post_type = 'attachment' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY wp_posts.post_date DESC LIMIT 0, 15
[posts] => Array ( )
)
Can anyone help, please?
$a = shortcode_atts( array( 'number' => '10'), $atts );
//Protect against arbitrary paged values
$temp = $the_query;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'attachment',
'post_parent' => get_the_ID(),
'posts_per_page'=>'15',
'paged' => $paged
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
$big = 999999999; // need an unlikely integer
$retuen_string .= paginate_links( array(
'base' => #add_query_arg('paged','%#%'),
'format' => '?paged=%#%',
'current' => $paged,
'total' => $the_query->max_num_pages
));
wp_reset_postdata();
$the_query = null;
$the_query = $temp;
else :
_e( 'Sorry, no posts matched your criteria.' );
endif;
I found the solution.
Had to add 'post_status' => 'inherit' in the $args.
Like this.
$args = array(
'post_type' => 'attachment',
'post_parent' => get_the_ID(),
'posts_per_page'=>'15',
'paged' => $paged,
'post_status' => 'inherit'
);
OMG. so simple.
The code was nothing wrong.
I just passed the wrong arguments...

printing all the “maps” related to the content type

my content type, plan to have one or more 'maps'.
With the following php code, I can take all their link:
<?php print render($content['field_mappe_di_riferimento']); ?>
and I get the following result:
I would like to put before the link the image that is within the content type "Mappa", entered in the field "field_immagine_min."
this is the structure of my content type:
this is the content type "Mappa":
with: "print_r($field_mappe_di_riferimento);" I get it:
Array ( [0] => Array ( [nid] => 30 [access] => 1 [node] => stdClass Object ( [vid] => 30 [uid] => 1 [title] => Carta di Marignano, primo giorno, 13 Settembre 1515 [log] => [status] => 1 [comment] => 1 [promote] => 1 [sticky] => 0 [nid] => 30 [type] => mappa [language] => en [created] => 1352066743 [changed] => 1352127342 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1352127342 [revision_uid] => 1 [field_battaglia_di_riferimento] => Array ( [und] => Array ( [0] => Array ( [nid] => 2 ) ) ) [field_immagine_std] => Array ( [und] => Array ( [0] => Array ( [fid] => 23 [alt] => [title] => [width] => 672 [height] => 554 [uid] => 1 [filename] => polesella.jpg [uri] => public://polesella_0.jpg [filemime] => image/jpeg [filesize] => 337755 [status] => 1 [timestamp] => 1352066743 [rdf_mapping] => Array ( ) ) ) ) [field_immagine_min] => Array ( [und] => Array ( [0] => Array ( [fid] => 24 [alt] => [title] => [width] => 150 [height] => 110 [uid] => 1 [filename] => polesella_small.jpg [uri] => public://polesella_small.jpg [filemime] => image/jpeg [filesize] => 10672 [status] => 1 [timestamp] => 1352066743 [rdf_mapping] => Array ( ) ) ) ) [field_testo_opzionale] => Array ( [und] => Array ( [0] => Array ( [value] => pisello [format] => [safe_value] => pisello ) ) ) [rdf_mapping] => Array ( [rdftype] => Array ( [0] => sioc:Item 1 => foaf:Document ) [title] => Array ( [predicates] => Array ( [0] => dc:title ) ) [created] => Array ( [predicates] => Array ( [0] => dc:date 1 => dc:created ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [changed] => Array ( [predicates] => Array ( [0] => dc:modified ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [body] => Array ( [predicates] => Array ( [0] => content:encoded ) ) [uid] => Array ( [predicates] => Array ( [0] => sioc:has_creator ) [type] => rel ) [name] => Array ( [predicates] => Array ( [0] => foaf:name ) ) [comment_count] => Array ( [predicates] => Array ( [0] => sioc:num_replies ) [datatype] => xsd:integer ) [last_activity] => Array ( [predicates] => Array ( [0] => sioc:last_activity_date ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) ) [cid] => 0 [last_comment_timestamp] => 1352066743 [last_comment_name] => [last_comment_uid] => 1 [comment_count] => 0 [name] => ant [picture] => 0 [data] => b:0; ) ) )
so I tried to take the nid of the node that I want to take the picture, with success:
print ($content['field_mappe_di_riferimento']['#items'][0][nid]);
I get 30...
I found the solution:
$array = $content['field_mappe_di_riferimento']['#items'];
$k = 0;
foreach($array as $k => $v){
print($k);
//carico il nodo
$node = node_view(node_load($content['field_mappe_di_riferimento']['#items'][$k]['nid']));
//stampa l'img del nodo
print render($node['field_immagine_min']);
}

Resources