Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Excluding Category in Custom Post Type…

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #43353
    esquivelia
    Participant

    I believe I would have to ad or change something (probably in the array) in this bit of code?

    if($my_post_type == CUSTOM_POST_TYPE1){
    $my_post_cat = ‘placecategory’;
    }else{
    $my_post_cat = ‘eventcategory’;
    }

    $args=array($my_post_cat => $category,
    ‘post_type’ => $my_post_type,
    ‘posts_per_page’ => $number,
    ‘ignore_sticky_posts’=> 1);
    $my_query = null;
    $my_query = new WP_Query($args);

    if( $my_query->have_posts() ) { ?>

    ……..
    #128173
    benpearson
    Participant

    Hi esquivelia,

    If you want to **exclude** a category from the loop you would use something like the following instead

    ‘cat’ => -13

    Where 13 is the category id.

    To get the category id from the category slug do something like this

    $catObj = get_category_by_slug($my_post_cat);
    $catID = $catObj->term_id;
    ?>

    So then the parameter for WP_Query would become:

    ‘cat’ => $catID

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘Back End’ is closed to new topics and replies.