WP_Query returns nothing - wordpress

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...

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?

query WooCommerce orders within a date range through a custom meta key

I have a meta_key called delivered_date, in date format, example 2021-05-13 16:01:26.
If I MUST use wc_get_orders( $args ), how can I get all orders within a date range?
Tried using the meta_compare argument but it returns nothing:
// The 2 variables below will come from a datepicker widget
$start_date = "2021-05-10 0:00:00";
$end_date = "2021-05-30 11:59:59";
$args = array(
'type' => 'shop_order',
'limit' => 100,
'paginate' => true,
'page' => 1,
'orderby' => 'id',
'order' => 'DESC',
'meta_key' => 'delivered_date',
'meta_value' => array($start_date, $end_date),
'meta_compare' => 'BETWEEN',
);
$result = wc_get_orders( $args );
You are close, use date — Format a local time/date
So you get:
// The 2 variables below will come from a datepicker widget
$start_date = date( '2021-05-10 00:00:00' );
$end_date = date( '2021-05-30 11:59:59' );
$args = array(
'orderby' => 'id',
'order' => 'DESC',
'meta_key' => 'delivered_date',
'meta_value' => array( $start_date, $end_date ),
'meta_compare' => 'BETWEEN',
);
$orders = wc_get_orders( $args );
// NOT empty
if ( ! empty ( $orders ) ) {
foreach ( $orders as $order ) {
echo '<p>ID = ' . $order->get_id() . '</p>';
}
}
Result: ID = 2525, ID = 2524, ID = 2523

WC_Order does not return the product id by get_items() [duplicate]

This question already has answers here:
Get Order items and WC_Order_Item_Product in WooCommerce 3
(2 answers)
Closed 5 years ago.
I have tried to get the product id and the product name by the following code:
if ( $query->have_posts() ) {
$order_id = $query->posts[0]->ID;
$order = new WC_Order( $order_id );
$items = $order->get_items();
}
foreach ( $items as $item ) {
$product_id = $item['product_id'];
$product = wc_get_product( $item_id );
$product_name = $item['name'];
}
In the above code I got the product name, but the it is return 0 for $product_id. Is there any other method for this?
I can't find any solution for this.
My Edited Version:
When I tried this:
$order_id = $query->posts[0]->ID;
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$item_id = $item['product_id'];
$product_name = $item['name'];
print_r( $item ); exit;
}
I got this array:
WC_Order_Item_Product Object
(
[extra_data:protected] => Array
(
[product_id] => 0
[variation_id] => 0
[quantity] => 1
[tax_class] =>
[subtotal] => 0
[subtotal_tax] => 0
[total] => 0
[total_tax] => 0
[taxes] => Array
(
[subtotal] => Array
(
)
[total] => Array
(
)
)
)
[data:protected] => Array
(
[order_id] => 684
[name] => Dark Skirt "Erebos" - Large, White
[product_id] => 0
[variation_id] => 0
[quantity] => 1
[tax_class] =>
[subtotal] => 19
[subtotal_tax] => 0
[total] => 19
[total_tax] => 0
[taxes] => Array
(
[total] => Array
(
)
[subtotal] => Array
(
)
)
)
[cache_group:protected] => order-items
[meta_type:protected] => order_item
[object_type:protected] => order_item
[id:protected] => 1
[changes:protected] => Array
(
)
[object_read:protected] => 1
[default_data:protected] => Array
(
[order_id] => 0
[name] =>
[product_id] => 0
[variation_id] => 0
[quantity] => 1
[tax_class] =>
[subtotal] => 0
[subtotal_tax] => 0
[total] => 0
[total_tax] => 0
[taxes] => Array
(
[subtotal] => Array
(
)
[total] => Array
(
)
)
)
[data_store:protected] => WC_Data_Store Object
(
[instance:WC_Data_Store:private] => WC_Order_Item_Product_Data_Store Object
(
[internal_meta_keys:protected] => Array
(
[0] => _order_id
[1] => _name
[2] => _product_id
[3] => _variation_id
[4] => _quantity
[5] => _tax_class
[6] => _subtotal
[7] => _subtotal_tax
[8] => _total
[9] => _total_tax
[10] => _taxes
[11] => _product_id
[12] => _variation_id
[13] => _qty
[14] => _tax_class
[15] => _line_subtotal
[16] => _line_subtotal_tax
[17] => _line_total
[18] => _line_tax
[19] => _line_tax_data
)
[meta_type:protected] => order_item
[object_id_field_for_meta:protected] => order_item_id
)
[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-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
)
[current_class_name:WC_Data_Store:private] => WC_Order_Item_Product_Data_Store
[object_type:WC_Data_Store:private] => order-item-product
)
[meta_data:protected] => Array
(
)
)
Of you see the [data:protected] array, you can see this where the [product_id] is 0. That is the problem I guess. But what is the solution for this?
it should be:
foreach ( $items as $item ) {
$product = $item->get_product();
$product_id = $product->get_id();
}

Group results if meta_value is the same

I would like to group the results of my query if the meta_value is the same for any results. I would expect an output like this:
Group 1
- 1111
- 1111
- 1111
Group 2
- 1234
- 1234
Here is my query:
$args = array(
'post_type' => 'product-api',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'last_updated',
'value' => '',
'compare' => '!=',
)
),
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$count++;
$the_query->the_post();
$model = get_post_meta(get_the_id(), 'product_model', true);
echo $model;
}
}
wp_reset_postdata();
What would be the best approach to achieve this?

WP_Query with previous_posts_link() and next_posts_link(

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>

Resources