Wordpress Query by Category using Post Slug

I am trying to query some posts by the category slug (and parent post category slug)I can get the slug using <?php ec

I am trying to query some posts by the category slug (and parent post category slug)

I can get the slug using

<?php echo $post->post_name; ?>

or

<?php $post_data = get_post($post->post_parent);
$parent_slug = $post_data->post_name;
echo $parent_slug; ?>

I would like to insert that into the query below:

<?php $query = new WP_Query(array(
'post_type' => 'offers',
'category_name' => 'PAGE-SLUG-event'
)); ?>

So effectively I am trying to do this, which doesn't obviously work.

 <?php $query = new WP_Query(array(
 'post_type' => 'offers',
 'category_name' => '<?php echo $post->post_name; ?>-event'
 )); ?>

I am trying to query some posts by the category slug (and parent post category slug)

I can get the slug using

<?php echo $post->post_name; ?>

or

<?php $post_data = get_post($post->post_parent);
$parent_slug = $post_data->post_name;
echo $parent_slug; ?>

I would like to insert that into the query below:

<?php $query = new WP_Query(array(
'post_type' => 'offers',
'category_name' => 'PAGE-SLUG-event'
)); ?>

So effectively I am trying to do this, which doesn't obviously work.

 <?php $query = new WP_Query(array(
 'post_type' => 'offers',
 'category_name' => '<?php echo $post->post_name; ?>-event'
 )); ?>
Share Improve this question asked Mar 11, 2020 at 15:41 A RimbaudA Rimbaud 1 1
  • 'category_name' => $post->post_name . '-event' is the correct way to pass that slug appended with -event. – Sally CJ Commented Mar 11, 2020 at 15:54
Add a comment  | 

1 Answer 1

Reset to default 0

function namefunctions(){ $args = array( 'post_type' => 'CPT', 'posts_per_page' => -1, ); $recetax = new wp_query($args); while ( $recetax->have_posts()): $recetax->the_post(); ?>

/* your content of ctp*/

<?php endwhile; wp_reset_query();  

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744678641a4587479.html

相关推荐

  • Wordpress Query by Category using Post Slug

    I am trying to query some posts by the category slug (and parent post category slug)I can get the slug using <?php ec

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信