• Resolved supeherotim

    (@supeherotim)


    Hi,

    I’m trying to display custom posts which have 2 categories.

    One category will have the same slug as the page the posts are on and the other category will have the same slug as the parent page of the current page.

    I can’t find a way to display posts that have both those categories.

    Here is my code:

    <?php
    
    $post_data = get_post($post->post_parent);
    $parent_slug = $post_data->post_name;			
    					
    $page_slug = basename(get_permalink());
    					  
    // the query
    $wpb_all_query = new WP_Query(array('post_type'=>'businesses', 'post_status'=>'publish', 'category_name'=>basename(get_permalink())+$parent_slug, 'meta_key'=>'featured', 'orderby'=>'meta_value','order'=>'DESC', 'posts_per_page'=>-1)); ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Assuming you’re correctly getting category names from post slugs, pass a plus + concatenated list of term names as the “category_name” assignment. In PHP, string concatenation is achieved by the dot . operator, you cannot just throw in string characters amongst functions and variable statements without delimiting and concatenating.
    'category_name' => basename(get_permalink()) .'+'. $parent_slug,

    Thread Starter supeherotim

    (@supeherotim)

    Thanks so much for that! It’s fixed my problem.

    Moderator bcworkz

    (@bcworkz)

    You are welcome!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Include 2 Category Names in query’ is closed to new replies.