TransWikia.com

how to list all post that are in the custom taxonomy using $wpdb

WordPress Development Asked by Kenneth Gervacio on September 3, 2020

global $wpdb;

$custom_post_type = 'post'; // define your custom post type slug here

// A sql query to return all post titles
$results = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title FROM {$wpdb->posts} WHERE post_type = %s and post_status = 'publish' OR post_status ='draft' ", $custom_post_type ), ARRAY_A );

this will output all my post with publish and draft post

I’m trying to output post that are in the tag (tag1,tag2), adding these code to sql tag_ID = 2 OR tag_ID = 1

    "SELECT ID, post_title FROM {$wpdb->posts} WHERE post_type = %s and 
post_status = 'publish' and tag_ID = 2 OR tag_ID = 1 OR post_status ='draft' ", $custom_post_type ), ARRAY_A );

But it only return nothing?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP