Поддержка → Темы и шаблоны → Подправить запрос
Подправить запрос
-
<?php $posts = get_posts( [ 'category' => 76967, 'orderby' => date, 'numberposts' => 6, ] );?>
Есть такой запрос, нужно изменить его так , чтобы выдавал он посты с категорий 76967 + 2
(если обе эти категории присутствуют только), если ставить'category' => 76967,2
то получаем категории с одним из параметров (76967 или 2).-
Тема изменена 2 года, 1 месяц назад пользователем
rstrah.
-
Тема изменена 2 года, 1 месяц назад пользователем
-
попробуйте
[76967,2]
Не помогло , тоже самое что и ‘category’ => 76967,2
_____
Подсказали вот такой вариант но он не до конца решает проблему<?php $posts = new WP_Query( [ 'orderby' => 'date', 'posts_per_page' => 6, 'tax_query' => [ 'relation' => 'AND', [ 'taxonomy' => 'category', 'field' => 'id', 'terms' => array( 76967, 2 ), 'operator' => 'AND', ] ] ] );?>
Сам код страницы
<?php /* ---------------------------------------------------------------------------- the default page template */ get_header(); //set the template id, used to get the template specific settings $template_id = 'page'; $loop_sidebar_position = td_util::get_option('tds_' . $template_id . '_sidebar_pos'); //sidebar right is default (empty) //get theme panel variable for page comments side wide $td_enable_or_disable_page_comments = td_util::get_option('tds_disable_comments_pages'); //read the custom single post settings - this setting overids all of them $td_page = td_util::get_post_meta_array($post->ID, 'td_page'); if (!empty($td_page['td_sidebar_position'])) { $loop_sidebar_position = $td_page['td_sidebar_position']; } // sidebar position used to align the breadcrumb on sidebar left + sidebar first on mobile issue $td_sidebar_position = ''; if($loop_sidebar_position == 'sidebar_left') { $td_sidebar_position = 'td-sidebar-left'; } /** * detect the page builder */ $td_use_page_builder = td_global::is_page_builder_content(); $custom_pages = array (481255, 631368, 643491, 646345, 647245, 652577, 653283, 653288, 653291, 653292, 653293, 653294, 653295, 653296, 653297, 653730, 658654, 666499, 666615, 666603, 666609, 666613, 666612, 666618, 677940, 710477, 733160, 743547, 657934); //ids of custom pages. test page - 657934 if ( in_array($post->ID, $custom_pages) ) { // the page is rendered using the page builder template (no sidebars) if (have_posts()) { ?> <?php while ( have_posts() ) : the_post(); ?> <div class="td-main-content-wrap td-main-page-wrap td-container-wrap"> <div class="<?php if (!td_util::tdc_is_installed()) { echo 'td-container '; } ?>tdc-content-wrap"> <?php the_content(); ?> </div> <?php if($td_enable_or_disable_page_comments == 'show_comments') { ?> <div class="td-container"> <div class="td-pb-row"> <div class="td-pb-span12"> <?php comments_template('', true); ?> </div> </div> </div> <?php } ?> </div> <!-- /.td-main-content-wrap --> <?php endwhile; ?> <?php } } else { //no page builder detected, we load a default page template with sidebar / no sidebar ?> <div class="td-main-content-wrap td-container-wrap"> <div class="td-container tdc-content-wrap <?php echo $td_sidebar_position; ?>"> <!-- Кривая верстка при неиспользуемом bread crumb <div class="td-crumb-container"> <?php // echo td_page_generator::get_page_breadcrumbs(get_the_title()); ?> </div> --> <div class="td-pb-row"> <?php switch ($loop_sidebar_position) { default: ?> <div class="td-pb-span9 td-main-content" role="main"> <div class="td-ss-main-content"> <?php if (have_posts()) { while ( have_posts() ) : the_post(); ?> <div class="td-page-header"> <div class="td-block-title-wrap"> <h1 class="block-title"> <span class="td-pulldown-size"><?php the_title() ?></span> </h1> </div> </div> <div class="td-page-content"> <div class="content-wrapper"> <div class="gallery"> <!--<?php the_content(); endwhile;//end loop } ?>--> <?php echo do_shortcode('[slide-anything id=928644]'); ?> </div> <!-- Gallery end --> <!-- Description start --> <div class="description-wrapper"> <div class="district-description-wrapper"> <h3>Описание района</h3> <p>В 1930-е годы началось формирование Комаровской площади (ныне Якуба Коласа). Подверглись реконструкции так называемые Комаровские вилы (слияние бывших Логойского и Борисовского трактов) и началось возведение Института физкультуры. В 1939 году его здание уже вошло в строй. Минск постепенно разрастался. Учитывая значительный рост населения столицы (218 тысяч человек) Президиум ЦИК БССР 17 марта 1938 года принял Постановление об образовании в столице республики трех городских районов: Сталинского, Когановичского и Ворошиловского (с 2 ноября 1961 года — Советского района). В связи с расширением города Советский район делился два раза. Именно из его состава образовался сначала Первомайский, а затем и Центральный районы города Минска (1969 год).</p> </div> </div> <!-- Description end --> <!-- JS section start--> <script> var mySwiper = new Swiper('.swiper-container', { slidesPerView: 4, centeredSlides: true, spaceBetween: 10, breakpoints: { // when window width is <= 499px 450: { slidesPerView: 2, spaceBetweenSlides: 10, }, 550: { slidesPerView: 3, spaceBetweenSlides: 30, }, // when window width is <= 999px 999: { slidesPerView: 3, spaceBetweenSlides: 30, }, }, grabCursor: true, direction: 'horizontal', loop: true, // Navigation arrows navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev', }, // And if we need scrollbar scrollbar: { el: '.swiper-scrollbar', }, }) </script> <!-- JS section end--> <?php if ($posts) : $counter = 0;?> <div class="district-news"> <div class="td_block_wrap td_block_15 td-pb-border-top td_block_template_1 td-column-2 td_block_padding"> <div class="td-block-title-wrap"> <h4 class="block-title"> <span class="td-pulldown-size">Общество</span> </h4> </div> <div class="td_block_inner td-column-2"> <div class="td-block-row"> <?php $posts = new WP_Query( [ 'orderby' => 'date', 'posts_per_page' => 6, 'tax_query' => [ 'relation' => 'AND', [ 'taxonomy' => 'category', 'field' => 'id', 'terms' => array( 76967, 2 ), 'operator' => 'AND', ] ] ] );?> <?php foreach ($posts as $post) : setup_postdata ($post); $counter++; $thumbnail_attributes = wp_get_attachment_image_src( get_post_thumbnail_id(), 'thumbnail' );?> <div class="td-block-span4"> <div class="td_module_mx4 td_module_wrap td-animation-stack"> <div class="td-module-image"> <div class="td-module-thumb"> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"> <img width="320" height="240" class="entry-thumb" src="<?php echo $thumbnail_attributes[0];?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>"> </a> </div> </div> <!--<div class="td-module-meta-info"> <span class="td-post-date"> <time class="entry-date updated td-module-date" ><?php the_date(); ?></time> </span> </div>--> <h3 class="entry-title td-module-title"> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a> </h3> </div> </div> <?php if($counter % 3 === 0) {echo '</div> <div class="td-block-row">'; }?> <?php endforeach; ?> </div> </div> </div> </div>
https://prnt.sc/1xgu18f Вот что получил
Ссылка на страницу https://minsknews.by/sovetskij-rajon/array( 76967, 2 )
то же самое, что[76967, 2]
'operator' => 'AND',
в tax_query означает, что будут выбраны те записи, которые и в одной рубрике, и в другой одновременно. Вам именно это нужно?Да
Но как я говорил раньше он не работает дельным образом (возможно проблема в шаблоне ), просьба помочь, спасибо!
Подсказали вот такой вариант но он не до конца решает проблему
…
он не работает дельным образомВ чём это выражается?
Записи отображаются вот так вот https://minsknews.by/sovetskij-rajon/
https://prnt.sc/1xkqfvd
- Тема «Подправить запрос» закрыта для новых ответов.