Can't add to $args $_GET value conditionally - WordPress - wordpress

In WordPress I have a loop with some $args array, which I want to be able to edit conditionally depending on a $_GET value.
$args = array(
'post_type' => 'property',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => $meta_query,
'tax_query' => $tax_query,
'fields' => 'ids',
'no_found_rows' => true,
'post__in' => $post__in,
'nopaging' => true
);
When adding to $args array like this:
if ( $_GET['prop_sort'] == 'price' ) {
$args['orderby'] = 'meta_value_num';
$args['order'] = 'ASC';
$args['meta_key'] = $prefix . 'price';
}
Nothing changes, and added to $args stuff gets ignored. However when I put code like this:
args = array(
'post_type' => 'property',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => $meta_query,
'tax_query' => $tax_query,
'fields' => 'ids',
'no_found_rows' => true,
'post__in' => $post__in,
'nopaging' => true
);
$args['orderby'] = 'meta_value_num';
$args['order'] = 'ASC';
$args['meta_key'] = $prefix . 'price';
Everything works as intended and posts are sorted by price from small to big.
What could be the problem?
EDIT:
Trying to influence query like this:
function myplugin_pre_get_posts( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->query_vars['post_type'] == 'property' ) {
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'ASC' );
$query->set( 'meta_key', 'sb_price' );
}
return $query;
}
add_action( 'pre_get_posts', 'myplugin_pre_get_posts', 1 );
Dones't work either (Undefined index: post_type in ..\functions.php on line 65).

Related

WooCommerce get all products with SKU

I want to get a list of products with sku and post id with this code, everything seems fine and ok with this code :
$statuses = array('publish', 'draft');
// Args on the main query for WC_Product_Query
$args = [
'status' => $statuses,
'orderby' => 'name',
'order' => 'ASC',
'limit' => -1,
];
$vendor_products = wc_get_products($args);
$list_array = array();
foreach ($vendor_products as $key => $product) {
if ($product->get_type() == "variable") {
// Args on product variations query for a variable product using a WP_Query
$args2 = array(
'post_parent' => $product->get_id(),
'post_type' => 'product_variation',
'orderby' => array( 'menu_order' => 'ASC', 'ID' => 'ASC' ),
'fields' => 'ids',
'numberposts' => -1,
);
foreach ( get_posts( $args2 ) as $child_id ) {
// get an instance of the WC_Variation_product Object
$variation = wc_get_product( $child_id );
if ( ! $variation || ! $variation->exists() ) {
continue;
}
$list_array[] = array(
'sku' => $variation->get_sku(),
'postid' => $variation->get_id()
);
}
} else {
$list_array[] = array(
'sku' => $product->get_sku(),
'postid' => $product->get_id()
);
}
}
I have total 1660 (470 published and 1,190 drafted) products but it's just returns 501 products and i don't know why!
this is my products in woocommerce:
this is the final result of query :
this is the result of Janki's code
// Woocommerce get all products
$args = array(
'post_type' => array('product','product_variation'),
'meta_key' => '_sku',
'post_status' => array('publish','draft'),
'meta_query' => array(
array(
'key' => '_sku',
'compare' => 'EXISTS',
),
),
);
$products = new WP_Query($args);
/* using this code you can get all products */
/* this may help you to get details */

Order posts by Title length in WP_QUERY

I am having problem ordering the posts based on the title length. Here is my code:
<?php
$terms = get_terms(array(
'taxonomy' => 'vendor_category',
'slug' => 'venues',
'hide_empty' => false
));
?>
<?php
foreach ($terms as $term) {
$eventargs = array(
'post_type' => 'vendor',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_key' => 'primary_category',
'meta_value' => $term->term_id,
);
$eventqry = new WP_Query($eventargs);
?>
How can i sort the posts based on the title length in ascending order.
You can save title length in post meta on save_post hook. and then you can retrieve post order by post meta value.
You can use save_post hook to save the post meta. put this code in your active theme.
//for existing vendors
add_action('admin_init', 'udpate_existing_vendor');
function udpate_existing_vendor(){
$existing_vendor_updated = get_option('existing_vendor_updated', 'no');
if( $existing_vendor_updated == 'no' ){
$vendor_args = array(
'post_type' => 'vendor',
'post_status' => 'publish',
'posts_per_page' => -1
);
$vendors = new WP_Query( $vendor_args );
if( $vendors->have_posts() ) {
while ( $vendors->have_posts() ) { $vendors->the_post();
$length = strlen( get_the_title() );
update_post_meta( get_the_ID(), 'title_length', $length );
} wp_reset_postdata();
}
update_option('existing_vendor_updated', 'yes');
}
}
// for new vendor
function save_vendor_title_length( $post_id ) {
$length = strlen( get_the_title( $post_id ) );
update_post_meta( $post_id, 'title_length', $length );
}
add_action( 'save_post_vendor', 'save_vendor_title_length');
Here your query will look like.
$terms = get_terms(array(
'taxonomy' => 'vendor_category',
'slug' => 'venues',
'hide_empty' => false
));
foreach ($terms as $term) {
$eventargs = array(
'post_type' => 'vendor',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_key' => 'title_length'
);
$eventqry = new WP_Query( $eventargs );
}

Exclude featured posts through custom query not working

I am trying to exclude posts from query and it is not working at all.
Here what I tried
<?php
$args = array(
'post_type' => 'videos-presentations',
'post_status' => 'publish',
'posts_per_page' => 4,
'paged' => $paged,
'meta_query' => array(
array(
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes',
'meta_compare' => '!='
)
)
);
$my_query = new WP_Query($args);
?>
Also Tried with
'meta_compare' => 'NOT EXIST'
and
'meta_compare' => 'NOT IN'
Any idea what I am doing wrong?
Got it. From here
It Works with just
'meta_query' => array(
array(
'key' => '_is_ns_featured_post',
'compare' => 'NOT EXISTS'
)
)
function exclude_posts ( $query ) {
$meta_query = $query->get( 'meta_query' );
$meta_query[] = array(
'key'=>'_is_ns_featured_post',
'value'=>'yes',
'compare'=>'!=',
);
$query->set( 'meta_query',$meta_query );
}
add_action( 'pre_get_posts', 'exclude_posts' );
place this code in functions.php file of active theme

custom wp_query where meta_key is variable or not needed

To make a custom post type filterable and sortable, i created a custom query that works based on variables in url, accessed by $_GET.
One specific situation breaks it. This is an (un)specified metakey. In the case of sort, the metakey is needed when sorting on a custom field. In the other cases, the metakey can be ignored. However, when i set the variable on empty, the query doesnt produce any posts. How to deal with an empty meta_key?
So far i tried to set the variable as empty ($variable ='');
I set the variable as null; I have used unset.
if (isset($_GET["key"]) || isset($_GET["orderby"])){
if (isset($_GET["key"])) {$key = $_GET["key"];}
else {$key = '';}
if (isset($_GET["value"])) {$value = $_GET["value"]; echo $value;}
else {$value = '';}
if (isset($_GET["orderby"])) {$orderby = $_GET["orderby"];}
if ($orderby='meta_value'){$meta_key='averagerating';}
else {$orderby='';$meta_key='';}
if (isset($_GET["order"])) {$order = $_GET["order"];}
else {$order='';}
$cat = get_queried_object();
if (!empty($cat) && !is_post_type_archive('bedrijf')){
$category = $cat->name;
}
if (is_post_type_archive('bedrijf')){
$category = '';
$terms = get_terms( 'bedrijfs-category' );
// convert array of term objects to array of term IDs
$category = wp_list_pluck( $terms, 'name' );
}
global $post;
$the_query ='';
$args = array(
'post_type' => 'bedrijf',
'posts_per_page' => 10,
'meta_key' => ''.$meta_key.'',
'orderby' => ''.$orderby.'',
'order' => ''.$order.'',
'tax_query' => array(
array(
'taxonomy' => 'bedrijfs-category',
'field' => 'name',
'terms' => $category
)
),
'meta_query' => array(
array(
'key'=> ''.$key.'',
'value' => ''.$value.'',
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query($args); }
WHat i would like is a solution for inserting an empty variable in the meta_key => ''.$meta_key.'' so the loop skips the meta_key part.
define meta_query or tax_query out of $args
and (example):
if($_GET['some_name'] !== ''){
$meta_query = array(
array(
'taxonomy' => 'bedrijfs-category',
'field' => 'name',
'terms' => $category
)
);
//define args with some_one para exists
$args = array(
'post_type' => 'bedrijf',
'posts_per_page' => 10,
'meta_key' => ''.$meta_key.'',
'orderby' => ''.$orderby.'',
'order' => ''.$order.'',
'meta_query' => $meta_query
);
}else{
//define args with some_one para not exists
$args = array(
'post_type' => 'bedrijf',
'posts_per_page' => 10,
'meta_key' => ''.$meta_key.'',
'orderby' => ''.$orderby.'',
'order' => ''.$order.''
);
}
tax_query is like this

how to add another list parameter for meta_query

here is my wordpress new query loop $args
if (isset($_GET['list_shows'])) {
if ($_GET['list_shows'] == 'update') {
$orderby = 'modified';
$order = '';
} elseif ($_GET['list_shows'] == 'views') {
$orderby = 'meta_value_num';
$order = 'DESC';
} elseif ($_GET['list_shows'] == 'popularity') {
$orderby = 'comment_count';
$order = 'DESC';
}
} else {
$orderby = 'modified';
$order = '';
}
$argz=array(
'posts_per_page' => '-1',
'orderby'=>$orderby,
'order'=>$order,
'meta_query' => array(
array(
'key' => 'fragman',
'compare' => 'NOT EXISTS',
'posts_per_page' => '-1',
)
),
'date_query' => array(
array(
'column' => 'post_date_gmt',
'after' => '2 days ago',
),
'posts_per_page' => '-1',
),
);
the question is about ordering posts by meta_value_num , descending ?
my post views meta key = views, could we add this ordering parameter in this meta query ?
if it is, how can we do it. ?
Thanks.
You can pass like this
global $wp_query;
$args = array(
'meta_key' => 'views',
'orderby' => 'meta_value meta_value_num',
'order' => 'DESC'
);
query_posts( array_merge( $args , $wp_query->query ) );
I hope this will work for you.

Resources