How to get fast Wordpress data using wpdb->get_results

Asked

Viewed 46 times

-1

Personal the code below it lists the results without repeating the post however need it to list containing the custom field with the following values:
meta_key "type"
meta_value "snack"

<?php
    global $wpdb, $table_prefix;
    $arquivos = $wpdb->get_results( '
        SELECT *
        FROM '.$table_prefix.'posts
        WHERE
            post_type = "arquivos"
            AND post_status = "publish"
        GROUP BY post_title
    ' );
?>
<?php foreach ( $arquivos as $key => $value ): ?>
    <?php echo $value->post_title ?>
<?php endforeach ?>

  • Why are you using SQL directly in the code? You can do any query with Wp_query

1 answer

0

You can make this query using Wp_query

$args = array(
    'post_type' => 'arquivos',
    'post_status' => array('publish'),
    'meta_query' => array(
        array(
            'key'     => 'tipo',
            'value'   => 'lanche',
            'compare' => '=',
        ),
    ),
);

$arquivos = new WP_Query($args);
foreach ($arquivos as $key => $value){
    echo $value->post_title;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.