How to Show WP-eCommerce Products on Blog Main Page

If you are using the WP-eCommerce plugin to sell items in support of your WordPress blog you may have been wondering if it’s possible to have your products show up on your main page intermixed with your regular blog posts.

The WordPress main post “loop” can be adjusted using the pre_get_posts filter.

The filter lets you change how WordPress builds the list of posts displayed for your blog.

You can add code like what is below to a file in your mu-plugins directory or theme functions.php file.

add_action( 'pre_get_posts', 'pbci_show_products_in_loop' );

/**
 * @param WP_Query $query
 */
function pbci_show_products_in_loop( $query ) {
    if ( ! is_admin() ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'post_type', array( 'post', 'wpsc-product' ) );
            $query->set( 'orderby', 'modified' );
            $query->set( 'order', 'DESC' );
        }

        if ( $query->is_main_query() && $query->is_search ) {
            $query->set( 'post_type', array( 'post', 'wpsc-product' ) );
        }
    }
}


Notice the check if ( ! is_admin() ) {.  This is to be sure we don't change the post listings in the WordPress dashboard.

The check if ( $query->is_home() && $query->is_main_query() ) { tests to see if we are on the main page in the main loop.

Then $query->set( 'post_type', array( 'post', 'wpsc-product' ) ); sets the desired post types to regular posts and product posts.

If you don’t want to play with the code, but want to give this idea a try, we have put the code into a FREE PLUGIN in our store.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.