• Resolved jklyn

    (@jklyn)


    Hi,
    I want to display my product randomly. I tried by doing RAND() instead of desc but it didn’t work. And I don’t understand the term offset. What does its value 0 mean? Here is my code.

    $arg_product=array(
              'post_type' => 'product',
              'posts_per_page' => 3,
              'offset' => 0,
              'order_by'=> 'date',
              'order'=>'desc',
              'post_status'=>'publish',

    Thank You

Viewing 8 replies - 1 through 8 (of 8 total)
  • try

    'orderby' => 'rand'

    Offset: offset (int) – number of post to displace or pass over. Warning: Setting the offset parameter overrides/ignores the paged parameter and breaks pagination. The ‘offset’ parameter is ignored when ‘posts_per_page’=>-1 (show all posts) is used.
    source: codex

    Thread Starter jklyn

    (@jklyn)

    Nope… It didn’t work.

    Thread Starter jklyn

    (@jklyn)

    Oh it worked!! I just forgot to remove ‘order’=>’desc’ Thanks to you.

    I am posting an working random-posts-from-from-custom-taxonomy here.. I used it in my old theme and it is 100% functional. just clear the mess… sorry for messy snippet.

    <?php
    
    // get the custom post type's taxonomy terms
    
    $custom_taxterms = wp_get_object_terms( $post->ID, 'genre', array('fields' => 'ids') );
    // arguments
    $args = array(
    'post_type' => 'albums',
    'post_status' => 'publish',
    'posts_per_page' => 13, // you may edit this number
    'orderby' => 'rand',
    'tax_query' => array(
        array(
            'taxonomy' => 'genre',
            'field' => 'id',
            'terms' => $custom_taxterms
        )
    ),
    'post__not_in' => array ($post->ID),
    );
    $related_items = new WP_Query( $args );
    // loop over query
    if ($related_items->have_posts()) :
    echo '<div class="widget"><h4 class="related_head">Also Check Out</h4><ul class="related section group">';
    while ( $related_items->have_posts() ) : $related_items->the_post();
    ?>
        <li class="col2 x12"><div class="img"><a href="<?php the_permalink(); ?>"><?php
    if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    	the_post_thumbnail( 'small_thumb' );
    }
    else { ?> <img width="60" src="/mp3/albumcover_min.jpg" alt="<?php the_title(); ?> Album thumb image" />
    <?php }
    ?></a></div><div class="related_content"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a><br/>
    <?php echo get_post_meta($post->ID, 'wpcf-total-songs', true); ?> Songs</div></li>
    <?php
    endwhile;
    echo '</ul></div>';
    endif;
    // Reset Post Data
    wp_reset_postdata();
    ?>

    Thread Starter jklyn

    (@jklyn)

    Thank you. It already worked. I want to ask u one more thing. I’ve CPT called product and I’ve one field called slogan. What I want to do is if the field is not empty then show it’s content other wise not to show this field. Can you write a code for this?

    if(get_post_meta($post->ID, 'meta-key', true))
        echo '<div class="whatever html u want">'. get_post_meta($post->ID, 'meta-key', true) .'</div>';
    }

    u can find meta key by inspecting the field in post editor. meta key usually be there as a class/id.

    Thread Starter jklyn

    (@jklyn)

    Thanks it worked.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to display post type randomly in arguments?’ is closed to new replies.