Wordpress使用args自定义选择查询转换为Wp_Query

时间:2015-03-30 10:40:32

标签: wordpress

任何人都可以帮我用args在Wp_query结构中转换下面的查询。

select post.ID, post.post_title, post.post_status, post.post_date, post.post_modified,post.post_author,ads.pack_name
    from wp_posts as post
        join wp_ads as ads on post.ID = ads.post_id
            where post.post_type = 'ad_listing'
            and post.post_status = 'publish'
            order by pack_id Desc

其中Wp_ads是由我创建的额外表而不是默认的WordPress表。我想将其转换为这种格式,以便循环和结构化很容易。

// WP_Query arguments
$args = array (
);

// The Query
$query = new WP_Query($args

我的格式查询,以便我可以使用have_post进行循环.Below是带有select查询的代码,不适用于分页。

   <?php
global $cp_options;
global $wpdb;
?>
<!-- Custom query for ads ordering {Featured,Urgent,Free} -->
<?php 

$querystr_AllPosts = "
    select post.ID, post.post_title, post.post_status, post.post_date, post.post_modified,post.post_author,ads.pack_name
        from wp_posts as post
            join wp_ads as ads on post.ID = ads.post_id
                where post.post_type = 'ad_listing'
                and post.post_status = 'publish'
                order by pack_id Desc
    ";
    $result_AllPosts = $wpdb->get_results($querystr_AllPosts,OBJECT);

    /*Pagination Code*/
    $ppp = intval(get_query_var('posts_per_page'));
    $wp_query->found_posts = count($result_AllPosts);
    $wp_query->max_num_pages = ceil($wp_query->found_posts / $ppp);
    $on_page = intval(get_query_var('paged'));
        if($on_page == 0)
        { 
            $on_page = 1;
         }      
    $offset = ($on_page-1) * $ppp;
    /*End Of Pagination Code*/

    $querystr_SelectedPosts = "
    select post.ID, post.post_title, post.post_status, post.post_date, post.post_modified,post.post_author,ads.pack_name
        from wp_posts as post
            join wp_ads as ads on post.ID = ads.post_id
                where post.post_type = 'ad_listing'
                and post.post_status = 'publish'
                order by pack_id Desc LIMIT $ppp OFFSET $offset
    ";
    $result_SelectedPosts = $wpdb->get_results($querystr_SelectedPosts,OBJECT);
    foreach($result_SelectedPosts as $post):
    setup_postdata($post);
?>

<?php appthemes_before_loop(); ?>


        <?php appthemes_before_post(); ?>

        <div class="post-block-out <?php cp_display_style( 'featured' ); ?>">

            <div class="post-block">

                <div class="post-left">

                    <?php if ( $cp_options->ad_images ) cp_ad_loop_thumbnail(); ?>

                </div>

                <div class="<?php cp_display_style( array( 'ad_images', 'ad_class' ) ); ?>">

                    <?php appthemes_before_post_title(); ?>

                    <h3><a href="<?php the_permalink(); ?>"><?php if ( mb_strlen( get_the_title() ) >= 75 ) echo mb_substr( get_the_title(), 0, 75 ).'...'; else the_title(); ?></a></h3>

                    <div class="clr"></div>

                    <?php appthemes_after_post_title(); ?>

                    <div class="clr"></div>

                    <?php appthemes_before_post_content(); ?>

                    <p class="post-desc"><?php echo cp_get_content_preview( 160 ); ?></p>

                    <?php appthemes_after_post_content(); ?>

                    <div class="clr"></div>

                </div>

                <div class="clr"></div>

            </div><!-- /post-block -->

        </div><!-- /post-block-out -->

        <?php appthemes_after_post(); ?>



    <?php appthemes_after_endwhile(); ?>


<?php appthemes_after_loop(); ?>
<?php endforeach;?>
<?php wp_pagenavi();?>

<?php wp_reset_query(); ?>

0 个答案:

没有答案