使用自定义META提交的自定义邮政类型帖子值WordPress

发布于 2025-01-24 12:44:56 字数 487 浏览 2 评论 0原文


$posts = new WP_Query( [
        'post_type'  => 'mock_test',
        'meta_query' => array(
        'key'     => 'selectedchapter',
        'value' => 15
        ),
    ] );

 echo "<pre>";
 print_r($posts);
 echo "</pre>";

我需要获取在自定义元框中选择第2章的帖子。这个WP_QUERY对我不起作用,我会收到所有帖子。 请参阅随附的屏幕截图( https://prnt.sc/vuzw6lhw44mr )。


$posts = new WP_Query( [
        'post_type'  => 'mock_test',
        'meta_query' => array(
        'key'     => 'selectedchapter',
        'value' => 15
        ),
    ] );

 echo "<pre>";
 print_r($posts);
 echo "</pre>";

I need to get posts which have selected chapter 2 in custom meta box. This WP_Query not working for me, I'm getting all the posts.
see attached screenshot (https://prnt.sc/vuzw6lhW44mR) of metabox html structure.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等往事风中吹 2025-01-31 12:44:56

这是一种获取帖子的简单方法,即自定义字段键为“颜色”,自定义字段值为“蓝色”:

   $args = array(
       'post_type' => 'product',
        'meta_key' => 'color',
       'meta_value' => 'blue'
     );
    $wp_query = new WP_Query( $args );

This is a simple way to get post where the custom field key is 'color' and the custom field value is ‘blue’:

   $args = array(
       'post_type' => 'product',
        'meta_key' => 'color',
       'meta_value' => 'blue'
     );
    $wp_query = new WP_Query( $args );
梅窗月明清似水 2025-01-31 12:44:56

尝试这些代码

$args = array(  
        'post_type' => 'mock_test', 
    );

    $loop = new WP_Query( $args ); 
        
    while ( $loop->have_posts() ) : $loop->the_post(); 

        print the_title(); 
        the_excerpt(); 
        $meta_print_value=get_post_meta(get_the_ID(),'selectedchapter',true);
        echo $meta_print_value;

    endwhile;

Try these code

$args = array(  
        'post_type' => 'mock_test', 
    );

    $loop = new WP_Query( $args ); 
        
    while ( $loop->have_posts() ) : $loop->the_post(); 

        print the_title(); 
        the_excerpt(); 
        $meta_print_value=get_post_meta(get_the_ID(),'selectedchapter',true);
        echo $meta_print_value;

    endwhile;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文