I have some problem with direct query in my wordpress single page.
I have 2 custom post type 'book' and 'author' I using metabox to connect them together. when user go to book custom post and adding new book. he must use check box metabox to determined which author write this book.
I have another page in my website which show author profile. in this page user can see books of author, my codes can save author for each book in database and read them perfectly but I don't know how can I get books of any author. I try to use direct query to database but it return empty array.
here is post_meta table
and here is my code for this part
<?php
get_header(); ?>
<div class="head-style col-md-12 col-sm-12 col-xs-12">
<?php $args = array( 'post_type' => 'author');
$loop = new WP_Query( $args );
while ( have_posts() ) : the_post(); ?>
<div class="title-pack col-md-12 col-sm-12 col-xs-12">
<span class="line visible-sm-block"></span>
<span class="visible-sm-block tittle-style"><?php the_title(); ?></span>
</div>
<div class="row writer-crit">
<div class="writer-crit-box col-md-9 col-sm-8 col-xs-12">
<div class="col-md-11 col-sm-11 col-xs-12 pull-right">
<div class="writer-bio pull-right col-md-12 col-sm-12 col-xs-12">
<?php the_post_thumbnail('post-thumbnail',array('class' => 'pull-right')); ?>
<div class="writer-content-bio col-md-8 col-sm-8 col-xs-12 pull-right">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
<div class="col-md-12 col-sm-12 col-xs-12 pull-right">
<h3>کتابشناسی </h3>
<?php $ars = array( 'post_type' => 'book');
$loop = new WP_Query( $ars );
// for reading author which choose from cheak box in each book pages.
$post_id = get_the_ID();
$key = 'save-author-to-book';
$key2='save-trans-to-book';
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<?php
$fivesdrafts =
$wpdb->get_results(
$wpdb->prepare( "
SELECT post_id
FROM $wpdb->post_meta
WHERE meta_key = 'save-trans-to-book'
AND meta_value LIKE %d
",
$post_id
)
);
var_dump($fivesdrafts);
?>
</div>
</div>
</div>
</div>
<?php endwhile; // End of the loop. ?>
meta_query
argument ofWP_Query
?WP_Query
to get posts, so I thought you already know about it.WP_Query
has an argument,meta_query
, that you can use to get posts based on post meta fields, which is what you are trying to do, right?. See the code reference for meta_query.