0

My homepage is displaing my listings( my custom post type) by the order i enter them. I would like my listings that have the custom taxonomy "tag" (Special offer) to be displayed on my first page from my homepage and after that the rest of the listings exactly how they where before. I am new in to wordpress and hope i asked my question right

This is my homepage code

<div id="content">
<?php include (TEMPLATEPATH . '/lib/slider.php'); ?>    

<?php
$args = array(
'post_type' => 'listings',
'paged' => $paged,
'showposts' => 8 ,
'oferta' =>"oferta"
);
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>   
<div class="post propbox <?php if (++$counter % 2 == 0) { echo "lastbox"; }?> clearfix" id="post-<?php the_ID(); ?>">
<div class="archimg">

<?php  if( has_term( 'featured', 'type', $post->ID ) ) { ?>
<span class="featspan">Featured</span>
<?php } else if ( has_term( 'sold', 'type', $post->ID ) ){ ?>
<span class="soldspan">Sold</span>
<?php } else if ( has_term( 'reduced', 'type', $post->ID ) ){ ?>
<span class="redspan">Reduced</span>
<?php } ?>

<?php
    if ( has_post_thumbnail() ) { ?>
    <a href="<?php the_permalink() ?>"><img class="propimg" src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php get_image_url(); ?>&amp;h=180&amp;w=310&amp;zc=1" alt=""/></a>
        <?php } else { ?>
    <a href="<?php the_permalink() ?>"><img class="propimg" src="<?php bloginfo('template_directory'); ?>/images/dummy.jpg" alt="" /></a>
<?php } ?>
</div>
<div class="cover">
    <div class="title">
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    </div>
    <div class="propmeta">
    <div class="proplist"><span>Price</span> <span class="propval"> <?php $price=get_post_meta($post->ID, 'wtf_price', true); echo $price; ?></span></div>
    <div class="proplist"><span>Location</span> <span class="propval"> <?php echo get_the_term_list( $post->ID, 'location', '', ' ', '' ); ?></span></div>
    <div class="proplist"><span>Property type</span> <span class="propval"><?php echo get_the_term_list( $post->ID, 'property', '', ' ', '' ); ?></span></div>
    <div class="proplist"><span>Area</span> <span class="propval"> <?php echo get_the_term_list( $post->ID, 'area', '', ' ', '' ); ?></span></div>
    </div>
    <div class="entry">
        <?php wpe_excerpt('wpe_excerptlength_archive', ''); ?>
        <a class="morer" href="<?php the_permalink() ?>">Check this</a>
        <div class="clear"></div>
    </div>
</div>
</div>

<?php endwhile; ?>

<div class="clear"></div>

<?php getpagenavi(); ?>

<?php $wp_query = null; $wp_query = $temp;?>

</div>

This is my main content content sorry how can i rearenge this so it fits my needs

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
Bogdan Bătrânu
  • 11
  • 1
  • 1
  • 4

4 Answers4

6

you can use tax_query like by here

$args = get_posts( array(
    'post_type' => 'my_post_type',
    'tax_query' => array(
        array(
            'taxonomy' => 'my_taxonomy',
            'field' => 'slug',
            'terms' => 'webdesign'
        )
    )
) );

$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);
Jothi Kannan
  • 3,320
  • 6
  • 40
  • 77
3

You have to pass the arguments array to you $wp_query->query($args); in which you define the tag name or category name , taxonomy of tag or taxonomy of category

$args = array(
'post_type' => 'your custom post type name',
'paged' => $paged,
'showposts' => 8 ,
'your custom goes here taxonomy' =>"tag name or category name"
);
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query($args);

Generally taxonomy name is what you defined in register_taxonomy('here_goes_taxonomy',array('post_type'), array(...))

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
  • i think i got the code wrong my code that display the listings is the edited question. from what you told me i understand that i have to add those lines in my function.php, if so where exactly? – Bogdan Bătrânu Sep 07 '13 at 10:36
  • No you don't have to add anything in functions.php because you have already created a custom posttype right? i am just asking you to replace your `wp_query` code with mine and also add right details of your post type and taxonomy, taxonomy name is that when you have created your post type you have register the taxonomy so copy the name of taxonomy from there :) – M Khalid Junaid Sep 07 '13 at 10:48
  • i used your code as it is show in my edited question: it is showing only the listings that have "oferta" taxonomy cheked the rest are not showing and beside that my footer is gone – Bogdan Bătrânu Sep 07 '13 at 14:16
0

All the answers above are great, but you don't necessarily need to pass the tax_query array. Once you have created your custom taxonomy the right way, you can do the following.

<?php
$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;

$loop = new WP_Query([
 'post_type'            => 'your_custom_post_type',
 'post_status'          => 'publish', // fetch only published posts
 'posts_per_page'       => get_option('posts_per_page') ?: 10, // number of posts to fetch
 'paged'                => $paged
 'your_custom_taxonomy' => 'your_custom_taxonomy_slug_or_name'
]);

// WordPress pagination doesn't work on custom query
// Let's make it work
$temp_query = $wp_query; // store $wp_query in a temporary variable
$wp_query   = null; // set it to null
$wp_query   = $loop; // store your custom query to $wp_query



if ( $loop->have_posts() ) { // Let's check if we have some posts

    while( $loop->have_posts() ){ 
     $loop->the_post();
        // add your markup here
     }

 }

 wp_reset_postdata();

 $wp_query = null;
 $wp_query = $temp_query;  // store back the original $wp_query
Dharman
  • 30,962
  • 25
  • 85
  • 135
nixx
  • 167
  • 1
  • 8
0
                         <?php  $cat_terms = get_terms(
                                      array('mobile_category'),
                                        array(
                    'hide_empty'    => false,
                    'orderby'       => 'name',
                    'order'         => 'ASC',
                    'number'        => 2 ) );
                        if( $cat_terms ) :  ?>
             
                    
                             <?php  foreach( $cat_terms as $term ) :
                                   $args = array(
                                            'post_type'=> 'mobile',
                                             'posts_per_page'=> 2, 
                                              'post_status'=> 'publish',
                                    'tax_query'=> array(
                                            array(
                                               'taxonomy' => 'mobile_category',
                                                'field'  => 'slug',
                                                 'terms' => $term->slug,),  ),
                                                  'ignore_sticky_posts' => true 
                                                   );
                                     $_posts = new WP_Query( $args );
                         if( $_posts->have_posts() ) :
                        while( $_posts->have_posts() ) : $_posts->the_post(); ?>
                            <span class="color2"><?php echo $term->name ; ?></span>
                                    
                      
                            <?php   endwhile;
                                        endif;
                                  wp_reset_postdata(); 

                             endforeach; ?>
                        
                <?php endif; ?>