简体   繁体   中英

Get Wordpress Post Data

I am using the following code in my Wordpress website to get data on Woocommerce products:

$args = array( 'post_type' => 'product', 'posts_per_page' => 100, 'product_cat' => 'Tools' );
$loop = new WP_Query( $args );
echo $loop->get_posts();
wp_reset_query();  

This only gets data on the posts. How do I get other product data such as price. I would usually get this using get_post_meta() . How do I get the post meta data without using a loop? Or is there one way to get all the data through one function or method?

You can use a custom query to get meta data without loop.

global $wpdb;
$query = "SELECT * FROM wp_posts as p, wp_postmeta as m WHERE p.post_type = 'product' 
AND p.post_status = 'publish' AND p.ID = m.post_id AND product_cat = 'Tools'";
$result = $wpdb->get_results($query);

Hope this helps.

<?php
$args = array(
    'tax_query' => array(
                array(
                'taxonomy' => 'project_category', // your category taxonomy
                'field' => 'id',
                'terms' => '17',  
                )
        ),
    'post_type' => 'project', // your custom post name
    'orderby' => 'menu_order',
    'post_status' => 'publish',
    'order' => 'ASC',
    'posts_per_page' => -1
);
    query_posts($args);
    while (have_posts()) : the_post();

            //your code here

    endwhile;

    wp_reset_query();
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM