I am currently looping through all posts and displaying a post_meta value like this:
global $wpdb;
$table = $wpdb->prefix . 'postmeta';
$theid = get_the_id();
$getLowestPrice = $wpdb->get_results("SELECT * FROM $table WHERE meta_value = '$theid'");
foreach ( $getLowestPrice as $post ){
get_post_meta( $post->post_id, '_wholesale_price', false );
}
Is there a way to order the results, lowest -> highest? At the moment they are getting displayed randomly, or as they were entered.
use the following code
<?php
global $wpdb;
$table = $wpdb->prefix . 'postmeta';
$theid = get_the_id();
$getLowestPrice = $wpdb->get_results("SELECT * FROM $table WHERE meta_value = '$theid'");
$all_post = array();
foreach ( $getLowestPrice as $post ){
$all_post[] = $post->post_id;
}
$query = new WP_Query( array( 'post__in' => $all_post, 'orderby' => 'meta_value', 'meta_key' => '_wholesale_price','order' => 'ASC') );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<div>' . get_post_meta( $get_the_ID(), '_wholesale_price', false );() . '</div>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>`
This looks a bit wrong to me. If you're trying to order posts by the value of a meta key (unless I'm mistaken, that is what you're doing) then I would normally go about it using WP_Query()
global $wp_query;
$args = array(
'post_type' => '[YOUR POST TYPE]',
'meta_key' => '_wholesale_price',
'orderby' => 'meta_value meta_value_num', // meta_value_num for value
'order' => 'ASC' // or DSC for low/high
);
$wp_query - new WP_Query( $args );
if ( $wp_query->have_posts() ) {
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
// Your loop
}
}
Related
There are many pages, but how to get paging?
Two columns of content have two paging.
i need paging for Two columns of content.
how to do this?
and if you can tell me how to add the function of the comments, I will thank you even more.
look this:
https://stackoverflow.com/a/45451489/1266305
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'ID',
'order' => 'desc');
$blank_posts = array();
$content_post =array();
$posts = new WP_Query( $args );
if ( $posts->have_posts() ) :
while ( $posts->have_posts() ) : $posts->the_post();
global $post;
$content = get_the_content();
if(empty($content)) {
array_push( $blank_posts, $post);
}else{
array_push( $content_post, $post);
}
endwhile;
endif;
/* blank content posts */ /* loop */
if(!empty($blank_posts)){
foreach ($blank_posts as $pst) {
echo "<pre>"; print_r($blank_posts);
echo $pst->post_title;
}
};
/* have content posts */
if(!empty($content_post)){
foreach ($content_post as $pst) {
echo '<a href='. $pst->guid .'>' .$pst->post_title. '</a> ';
}
}
I made some changes in a plugin that creates meta boxes, i added tome default code into the "value" of the meta box.
The data is being sent outside. my problem is that now i need to press "update" on every woocommerce product for it to be permanently saved and sent. But i have 1500 products.
I have tried bulk updating from the general admin area, like Shawn here:
https://bobwp.com/bulk-edit-posts-wordpress/
It didn't work. so i tried this code:
$args = array(
'posts_per_page' => 1000,
'post_type' => 'product'
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$casa_id = $post->ID;
update_post_meta($post->ID, 'casa_id', $casa_id);
}
}
And also this:
function woo_zap_update_product_name() {
if ( ! is_admin() ) {
return;
}
$zap_query = new WP_Query( array( 'post_type' => 'product', 'posts_per_page' => -1 ) );
if ( $zap_query->have_posts() ) {
global $post;
while ( $zap_query->have_posts() ) {
$zap_query->the_post();
update_post_meta( $post->ID, '_wc_zap_product_name', $post->post_title );
}
wp_reset_postdata();
}
}
add_action( 'init', 'woo_zap_update_product_name' );
Didn't work as well.. any other ideas ?
Currently all posts is being listed by category,
i need to filter the list by a meta-key. I used the filter('posts_where') but all the query has been changed.
I need to add the WHERE condition in the existing sql that is being generated.
function get_filtered_post( $args, $meta, $value ){
$posts = get_posts( $args );
$ids = array();
foreach( $posts as $post ){
$id = $post->ID;
if( get_post_meta( $id, $meta, true ) == $value ){
$ids[] = $id;
}
}
return $ids;
}
$args = array( 'post_type' => 'post', 'posts_per_page' => -1 );
$IDofPost = get_filtered_post( $args, 'my-meta-key', 'the-metas-value' );
foreach( $IDofPost as $id ){
echo get_the_title( $id );
}
Hope this function will help. It returns post id with the given post meta values.
I want to get all user list of a specific category. I use WP_User_Query() function for the query and assign its returned value into a variable.When I use var_dump() then its show as like http://postimg.org/image/48st2h0jj/. How can I get the role of that user from that object. My code is below ..
$user_query = new WP_User_Query( array( 'role' => 'Administrator' ) );
foreach ($user_query as $user) {
$aa = $user->role;
echo $aa;
}
But It doesn't work as I want. How can I solve this problem.
Thanks...Anam
Try This
<?php
$args = array(
'role' => 'Administrator'
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo '<p>' . $user->role . '</p>';
}
} else {
echo 'No users found.';
}
?>
if it's Not Work Than Use This code
$administrator_query = new WP_User_Query(
array(
'role'=>'administrator',
)
);
$user_query= $administrator_query->get_results();
foreach ( $user_query->results as $user ) {
echo '<p>' . $user->role . '</p>';
}
$user_query = new WP_User_Query( array( 'role' => 'Administrator' ) );
foreach ( $user_query as $user ) {
$value = json_decode(json_encode($user),true);
echo $value['role']."<br>";
}
json_decode function will convert it into array format and then we can access them
After some long search I find my own solution... At first I have to make object as an array. Then I get the result from that array... here is my own code
$user_query = new WP_User_Query( array( 'role' => 'Administrator' ) );
$posts = $user_query->get_results();
foreach ($posts as $user) {
$aa = $user->roles[0];
echo $aa;
}
Thanks to all for answer my question.
Im having problems with retrieving product infos trough object functions in Woocommerce.
This is how I do:
public function table_data()
{
$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
$products = get_posts( $args );
$pfactory = new WC_Product_Factory();
foreach($products as $product)
{
$_product = $pfactory->get_product($product);
}
}
This returns product informations from wp_posts only, it won't give me the information stored in wp_postmeta.
I need the full information from all products (such as price, sku, stock etc.) in one array, but I seem to be missing something and Im unsure if it has with the hierarchy of the functions in my code. However I thought this was possible to do without SQL-queries.
Basicly, what im trying to do is a complete duplicate of product list in admin for listing products with information in the panel.
Thanks for all the help I can get.
Use WP_Query instead, which will give you access to the global WooCommerce $product variable from within the loop. From there, you can grab the price, sku, stock and all other kinds of data. http://docs.woothemes.com/wc-apidocs/class-WC_Product.html
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ): while ( $loop->have_posts() ): $loop->the_post();
global $product;
$price = $product->get_price_html();
$sku = $product->get_sku();
$stock = $product->get_stock_quantity();
endwhile; endif; wp_reset_postdata();
?>
First thing I came up with was this.
function products() {
return array_map('wc_get_product', get_posts(['post_type'=>'product','nopaging'=>true]));
}
I'd love to know from someone more experienced with WooCommerce if there is a better way
As of WooCommerce 3 you can do it using the wc_get_products function like this:
$args = array(
'status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'limit' => -1,
);
$products = wc_get_products($args);
if (count($products) > 0) {
foreach ($products as $product) {
echo $product->get_id() . '<br>';
echo $product->get_name() . '<br>';
echo $product->get_price_html() . '<br>';
echo $product->get_sku() . '<br>';
echo $product->get_stock_quantity() . '<br>';
}
}