最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Show search for data extracted from metabox

programmeradmin1浏览0评论

In custom post type book I have several books published I just want to show data obtained from metabox field but I have not been able to help me the code that I am showing does not work

   <?php $book=get_post_meta( get_the_ID(), 'name_ebook', true ); ?> return name ebook Paintree

            <?php 
                $loop_args = array(
                 'post_type' => 'book',
                 'order' => 'ASC',              
                 'meta_key' => '$book',  
                 'orderby' => 'meta_value',
               ); ?>

show only search Paintree ebook

In custom post type book I have several books published I just want to show data obtained from metabox field but I have not been able to help me the code that I am showing does not work

   <?php $book=get_post_meta( get_the_ID(), 'name_ebook', true ); ?> return name ebook Paintree

            <?php 
                $loop_args = array(
                 'post_type' => 'book',
                 'order' => 'ASC',              
                 'meta_key' => '$book',  
                 'orderby' => 'meta_value',
               ); ?>

show only search Paintree ebook

Share Improve this question asked Jun 4, 2020 at 23:26 StymarkStymark 372 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0
    $book_args = array(
    'post_type'        => 'book',
    'posts_per_page'   => -1,
    'meta_key'         => 'name_ebook',
    'meta_value'       => 'Paintree'
);

You're not using $book as a variable in your $loop_args, but as a string $book. Just remove the quotes around the variable in the args and it should work (assuming get_post_meta returns something and not just empty string).

$book = get_post_meta( get_the_ID(), 'name_ebook', true );
$loop_args = array(
  'post_type' => 'book',
  'order'     => 'ASC',              
  'meta_key'  => $book, // no quotes needed here
  'orderby'   => 'meta_value',
);
发布评论

评论列表(0)

  1. 暂无评论