我想列出最新的儿童连接帖子,按家长帖子排序。有可能吗?有人能帮我吗?我用一个例子来解释:假设我有一个剧集自定义帖子类型和一个剧集自定义帖子类型,它们都连接到帖子2。在戏剧档案中,我想按最新剧集列出所有剧集,并且只想在循环中显示一集。
我认为使用each_connected()可能是可行的,但是我不知道如何对$wp_query数组进行排序才能完成这项工作。
谢谢。
我现在正在使用这样的代码
<?php
global $post;
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$anime_series = array(
'post_type' => 'drama',
'posts_per_page' => 12,
'paged' => $paged,
'page' => $paged,
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'DESC'
);
$the_query = new WP_Query( $anime_series);
$extra = array(
'posts_per_page' => 1,
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'DESC'
);
p2p_type( 'drama_to_episode' )->each_connected( $the_query, $extra, 'episode' );
?>
</div>
<div class="last_episodes loaddub">
<ul class="items">
<?php if ( $the_query->have_posts() ) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<li>
<div class="img"> <a href="<?php echo the_permalink(); ?>" title="<?php echo the_title(); ?>"> <img alt="<?php echo the_title(); ?>" itemprop="photo" src="<?php echo get_the_post_thumbnail_url(); ?>"> </a>
<div class="type"><img src="<?php echo get_stylesheet_directory_uri(); ?>/images/SUB.png"></div>
</div>
<?php
foreach ( $post->episode as $post ) : setup_postdata( $post ); ?>
<p class="name"> <a href="<?php echo the_permalink(); ?>" title="<?php echo get_the_title($items['parent']); ?>"> <?php echo get_the_title($items['parent']); ?> </a> </p>
<p class="episode"> <?php echo the_field('short_name'); ?> </p>
<?php
endforeach;
?>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>
<?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?>
</p>
<?php endif; ?>
</ul>
</div>发布于 2018-01-05 16:16:13
试试这个简单的例子
?php
$args = array(
'post_parent' => 38,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'any'
);
$children = get_children( $args );
foreach($children as $post):
echo $post->post_title;
endforeach;
?>
希望能在你的需求中发挥作用!
发布于 2018-01-06 19:24:57
幸运的是,我自己解决了我的问题。我主要关心的是获得最新的与其父母相连的儿童帖子。在默认循环中之前,正在检索所有剧集(旧/新)。我为帖子插件创建了一个自定义的select查询。此查询返回最近连接的帖子的ID。工作代码如下所示。如果我使用了错误的编程标准,请纠正我。
global $post, $items, $wpdb;
$table_name = $wpdb->prefix . "p2p";
$query = 'SELECT p2p_from as anime, max(p2p_to) as episode FROM '.$table_name;
$results = $wpdb->get_results( $query.' GROUP BY p2p_from ORDER BY episode DESC' );然后,在foreach循环中,我通过ID检索了帖子标题、固定链接等。
https://stackoverflow.com/questions/48109154
复制相似问题