按ACF自定义字段从2个特定分类中查询所有POST和CPT

时间:2021-07-06 作者:sandra nanou

我需要构建一个复杂的查询。我想显示所有帖子,以及特定分类中的两个不同的CPT帖子。所有这些帖子都需要经过检查才能显示出来。该复选框添加了ACF。我的ACF复选框名为highlight,值必须为yes。

因此,我的查询应显示:

到目前为止,我尝试了以下方法:

query_posts(array(
            \'post_type\' => array(  \'post\', \'cpt1\', \'cpt2\' ),
             \'post_status\' => \'publish\',
             \'orderby\' => \'date\',
             \'order\' => \'DESC\',
             \'posts_per_page\' => 10,
             
             \'meta_query\' => array(
                \'relation\' => \'AND\',
                 array(
                     \'key\'     => \'highlight\',
                     \'value\'   => \'"yes"\',
                     \'compare\' => \'LIKE\'
                 )
                ),
                 \'tax_query\' => array(
                    \'relation\' => \'OR\',
                    array(
                        \'taxonomy\' => \'TAX1\',
                        \'field\'    => \'slug\',
                        \'terms\'    => \'valueTAX1\',
                    ),
                    array(
                        \'taxonomy\' => \'TAX2\',
                        \'field\'    => \'slug\',
                        \'terms\'    => \'valueTAX2\',
                    ),
                   
                ),
         )
但它不起作用:(

任何帮助都将不胜感激!

谢谢你,桑德拉

1 个回复
SO网友:sandra nanou

我终于找到了解决办法!

以下是查询,以防有人需要它:

 // first query
            $first_ids = get_posts(array(
                \'fields\'         => \'ids\',
                \'posts_per_page\' => \'10\',
                \'post_status\'    => \'publish\',
                \'post_type\'      => array(\'post\'),
                \'orderby\'        => \'date\',
                \'order\'          => \'DESC\',
              
            ));

            // second query
            $second_ids = get_posts(array(
                \'fields\'         => \'ids\',
                \'posts_per_page\' => \'10\',
                \'post_status\'    => \'publish\',
                \'post_type\'      => array(\'CPT1\'),
                \'orderby\'        => \'date\',
                \'order\'          => \'DESC\',
                \'tax_query\'      => array(array(
                    \'taxonomy\'       => \'TAX1\',
                    \'field\'          => \'slug\',
                    \'terms\'          =>  \'tax_value\',
                ))
            ));

            // third query
            $third_ids = get_posts(array(
                \'fields\'         => \'ids\',
                \'posts_per_page\' => \'10\',
                \'orderby\'        => \'date\',
                \'order\'          => \'DESC\',
               
                \'post_type\'      => array(\'CPT2\'),
               
                \'tax_query\'      => array(array(
                    \'taxonomy\'       => \'TAX2\',
                    \'field\'          => \'slug\',
                    \'terms\'          => \'TAX2value,
                ))
            ));

            // merging ids
            $post_ids_list = array_merge($first_ids, $second_ids, $third_ids);

            $querySlider = new WP_Query(array(
                \'post_type\' => \'any\',
                \'posts_per_page\' => \'10\',
                \'post__in\'  => $post_ids_list,
                \'post_status\'    => \'publish\',
                \'orderby\'   => \'date\',
                \'order\'     => \'DESC\',
                \'meta_query\' => array(
                    array(
                        \'key\'     => \'highlight\',
                        \'value\'   => \'"yes"\',
                        \'compare\' => \'LIKE\'
                    )
                ),
            ));
            ?>
           
我在这里找到了解决方案:Merge 2 args in one WP_Query and order it by date

相关推荐

Bulk edit for custom taxonomy

要创建如图所示的区域,我应该在下面进行什么样的更改<?php /* Portfolio Custom Post Type */ $args = array( \"label\" => \"Şarkı Sözü\", \"singular_label\" => \"Şarkı Sözü Kategorisi\", \'public\'