• Resolved hyplus

    (@hyplus)


    hello,
    I am trying to insert a code to create a random post in my homepage, excluding the categorie 46 using wordpress 2.3.1:

    <?php
    $not_this_cat = 46;
    $rand_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts LEFT JOIN $wpdb->post2cat ON ID = post_id WHERE category_id <> $not_this_cat AND post_status = 'publish' ORDER BY RAND() LIMIT 1");
    $wp_query->is_single = false;
    ?>

    The problem is that the tag post2cat is outdated.

    Someone could help with the new way to do

    Thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • Cathy Mitchell

    (@multitalentedmommy)

    there are oodles of random post plugins in the extend page.

    Thread Starter hyplus

    (@hyplus)

    this is not the question . Most of random post plugins are using post2cat tag for exclude a category. In version 2.3.1 is oudated

    Cathy Mitchell

    (@multitalentedmommy)

    https://rmarsh.com/plugins/random-posts/
    – one of many from a quick google search

    Thread Starter hyplus

    (@hyplus)

    you are not understanding:

    “I am trying to insert a code to create a random post in my homepage”

    not a random post list

    Kafkaesqui

    (@kafkaesqui)

    $rand_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts LEFT JOIN $wpdb->term_relationships ON ID = object_id WHERE term_taxonomy_id <> $not_this_cat AND post_status = 'publish' AND post_type = 'post' ORDER BY RAND() LIMIT 1");

    Info on the ‘category’ db changes:
    https://www.ads-software.com/support/topic/135564

    Thread Starter hyplus

    (@hyplus)

    Thanks Kafkaesqui, but it did not work.
    It ignores completely.
    I put this before the loop:

    <?php
    $not_this_cat = 46;
    $rand_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts LEFT JOIN $wpdb->term_relationships ON ID = object_id WHERE term_taxonomy_id <> $not_this_cat AND post_status = 'publish' AND post_type = 'post' ORDER BY RAND() LIMIT 1");
    ?>

    You can check if you want…

    Finally the solution I found was this:

    <?php
    query_posts('cat=-46&showposts=1&random=true');
    ?>

    found in this post:
    https://www.ads-software.com/support/topic/70359?replies=8

    Kafkaesqui

    (@kafkaesqui)

    random=true

    Hehehe. From now on everybody, this is the way to do it!

    I love how the devs keep adding cool stuff they don’t tell anyone about. :p

    (ignore my ramblings tonight)

    Thread Starter hyplus

    (@hyplus)

    As stated in the original article is placed with this code in a php plugin:

    <?php
    /*
    Plugin Name: Random Posts Query
    */
    function query_random_posts($query) {
    return query_posts($query . '&random=true');
    }
    class RandomPosts {
    function orderby($orderby) {
    if ( get_query_var('random') == 'true' )
    return "RAND()";
    else
    return $orderby;
    }
    function register_query_var($vars) {
    $vars[] = 'random';
    return $vars;
    }
    }
    add_filter( 'posts_orderby', array('RandomPosts', 'orderby') );
    add_filter( 'query_vars', array('RandomPosts', 'register_query_var') );
    ?>

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘pulling 1 random post excluding a specific category in wordpress 2.3’ is closed to new replies.