0

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

enter image description here

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. ?>
  • Why don't you use meta_query argument of WP_Query? – cybmeta Aug 31 '16 at 6:56
  • @cybmeta I'm new to wordpress and SQL query so can't find right way. so I asked it here maybe I can find something which help to solve this problem – mkafiyan Aug 31 '16 at 6:58
  • @cybmeta can you enplane what is meta_query dose? – mkafiyan Aug 31 '16 at 6:59
  • Sorry, I see in your code that you are already usgin 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. – cybmeta Aug 31 '16 at 14:33
0

If I understand correctly you wish to retrieve your meta data from the database? Try looking at this WP function: https://developer.wordpress.org/reference/functions/get_metadata/

This will allow you to get the meta data you require dependent on the type of data you want to get, such as post meta data or page meta data etc.

An additional read might be this Stackoverflow question from another user. There are some answers that show how to get the meta data and echo it out.

https://stackoverflow.com/questions/22216276/display-all-post-meta-keys-and-meta-values-of-the-same-post-id-in-wordpress

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.