• hi i just setup a random post thingy in my wordpress

    <?php
    add_action('init','random_add_rewrite');
    function random_add_rewrite() {
    global $wp;
    $wp->add_query_var('random');
    add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
    }
    add_action('template_redirect','random_template');
    function random_template() {
    if (get_query_var('random') == 1) {
    $posts = get_posts('post_type=post&orderby=rand&numberposts=1');
    foreach($posts as $post) {
    $link = get_permalink($post);
    }
    wp_redirect($link,307);
    exit;
    }
    }
    ?>

    and i want to exclude a category from the random link thingy is that possible ? and how to do it

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi,

    You can add &cat=-10 to your get_posts line, like this:

    $posts = get_posts('post_type=post&orderby=rand&numberposts=1&cat=-10');

    10 in this case is the category ID. The negative sign means exclude.

    How to exclude many categorys ID???

    Moderator bcworkz

    (@bcworkz)

    sotos, Did you try a comma delimited list? 'cat=-10,-11,-12'
    Don’t know if that’s proper, I don’t use string arguments.

    I’d suggest you also move away from string based arguments such as used in this thread and implement array based arguments instead. Then you could do array( 'cat' => array( -10, -11, -12 ),)
    or array( 'category__not_in' => array( 10, 11, 12 ),)
    Class_Reference/WP_Query#Category_Parameters

    Arrays enable much more elaborate query arguments and if properly formatted in code, are much easier to read than a long string of arguments.

    Also, don’t hesitate to start a new topic in these forums, it’s actually preferred over tagging on to old threads like is done in many other forums. WP is a bit different in so many ways!

    Hello bcworkz
    I try the cat=-1,-2, etc and doesnt work
    I not so experrt in php. I must remove this line ?
    $posts = get_posts(‘post_type=post&orderby=rand&numberposts=1&cat=-10’);

    and replace this? $query = new WP_Query( array( ‘cat’ => ‘-12,-34,-56’ ) );

    i have this code in my function.php

    /** Random Contest Button*/
    add_action(‘init’,’random_add_rewrite’);
    function random_add_rewrite() {
    global $wp;
    $wp->add_query_var(‘random’);
    add_rewrite_rule(‘random/?$’, ‘index.php?random=1’, ‘top’);
    }
    add_action(‘template_redirect’,’random_template’);
    function random_template() {
    if (get_query_var(‘random’) == 1) {
    $posts = get_posts(‘post_type=post&orderby=rand&numberposts=1&cat=-2’);
    foreach($posts as $post) {
    $link = get_permalink($post);
    }
    wp_redirect($link,307);
    exit;
    }
    }

    Moderator bcworkz

    (@bcworkz)

    Sorry for being rather vague, I mis-judged your skill level. Nothing wrong with being a novice, I just need to adjust. Let’s try again. Replace the entire $posts = get_posts(...); line with the following block:

    $posts = get_posts( array(
       /*'post_type' => 'post',*/
       'orderby' => 'rand',
       'posts_per_page' => 1,
       'category__not_in' => array(
          12, 34, 56,
        ),
    ));

    Besides converting to array form, let me explain the changes. I’ve commented out the post type argument because ‘post’ should be the default. It doesn’t hurt to have it, but it should be unnecessary. Just in case there’s a quirk about your site where it is not the default, simply remove the /* */ comment delimiters.

    orderby is fine, just converted to array form.

    numberposts is an outdated term, we’re using posts_per_page now, it’s a little more descriptive. Either way works though.

    cat would have worked the way you had it, but I prefer category__not_in because I think it’s more descriptive.

    Thank you for your time bcworkz finaly it works!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Hi i want to exclude a category’ is closed to new replies.