Test If WP_Query Object Is Empty - wordpress

How can I test to see if a WP_Query object does not return any matches? I'd like to be able to do something like this is a template:
<?php
$my_query = new WP_Query(array('post_type' => 'biographies'))
if( ***HOW DO I TEST $my_query HERE*** ) {
//if $my_query finds anything loop through it here
} else {
//if $my_query does not find anything
}
?>
EDIT
Better example, I want to only display the h2 if the query finds anything:
<?php
$outside_leasing_query = new WP_Query(array( 'post_type' => 'resin_biographies', 'tax_query' => array(
'relation' => 'AND',
array( 'taxonomy' => 'resin_buildings', 'field' => 'slug', 'terms' => $page_slug ),
array( 'taxonomy' => 'resin_leasing_companies', 'field' => 'slug', 'terms' => 'rubenstein-partners', 'operator' => 'NOT IN' )
) )); // resin_buildings taxonomy term slug must match page slug
?>
<h2>Outside Leasing Contacts</h2>
<?php while ( $outside_leasing_query->have_posts() ) : $outside_leasing_query->the_post(); ?>
<article <?php post_class('group'); ?>>
<?php
if( get_post_meta( $post->ID, '_biography_headshot', true ) != '' ) {
echo '<img class="contact-thumb" src="' . get_post_meta( $post->ID, '_biography_headshot', true ) . '" alt="'. get_the_title() .'" />';
} else {
echo '<img class="contact-thumb-placeholder" src="' . get_bloginfo('template_url') . '/images/default_headshot.jpg" alt="'. get_the_title() . '" />';
}
?>
<div class="contact-info">
<hgroup>
<?php the_title( '<h3>', '</h3>' ); ?>
<h4 class="contact-title"><?php echo get_post_meta( $post->ID, '_biography_title', true ); ?></h4>
</hgroup>
<div class="contact-address"><?php echo wpautop( get_post_meta( $post->ID, '_biography_address', true ) ); ?></div>
<div class="contact-tel"><span>T</span> <?php echo get_post_meta( $post->ID, '_biography_tel', true ); ?></div>
<?php if( get_post_meta( $post->ID, '_biography_fax', true ) != '' ) { ?>
<div class="contact-fax"><span>F</span> <?php echo get_post_meta( $post->ID, '_biography_fax', true ); ?></div>
<?php } ?>
<div class="contact-email"><?php echo get_post_meta( $post->ID, '_biography_email', true ); ?></div>
</div>
</article>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>

The standard Wordpress Loop does this for you using have_posts().
<?php
$my_query = new WP_Query(array('post_type' => 'biographies'));
if($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post();
?>
<!-- YOUR POST(S) -->
<?php endwhile; else : ?>
<p>No posts found</p>
<?php endif; ?>

I figure you could use:
!($my_query->have_posts())
so:
<?php
$my_query = new WP_Query(array('post_type' => 'biographies'))
if( !($my_query->have_posts())) {
//if $my_query finds anything loop through it here
} else {
//if $my_query does not find anything
}
?>

In your first example, where you have the comment:
//if $my_query does not find anything
Just set a parameter like this:
$my_query_found_something = 'not';
Then use it wherever you need like this:
if ($my_query_found_something == 'not') {
// keep looking
}

Related

Get grouped product highest price woocommerce

I am trying to get grouped products highest price so that i can show only highest price on custom template page.I am using WP_Query to get list of products.
Here is the snippet:
$series_term = $_GET['series'];
$args=array(
'series' => $term_name->slug,
'post_type' => 'product',
'posts_per_page' => 8,
'filter' =>$valuecol,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'hidden' ), // Don't display products in the knives category on the shop page
'operator' => 'NOT IN'
)
)
);
// $do_not_duplicate[] = $post->ID;
$my_query = null;
$my_query = new WP_Query($args);
/* Start the Loop */
while ($my_query->have_posts()) : $my_query->the_post();
$feat_prod_img = wp_get_attachment_image_src(get_post_thumbnail_id(), 'post-thumb-single');
$feat_product_img = wp_get_attachment_url( get_post_thumbnail_id($my_query->post->ID) );
?>
<div class="col-md-3 col-sm-4 col-xs-6">
<div class="product-detail">
<div class="product-thmbnial">
<a href="<?php the_permalink(); ?>">
<img src="<?php $src=$feat_product_img; echo image_product_resize_crop( $src, $w=440, $h=275, $dest = null, $override = false, $createNewIfExists = false ); ?>" alt="<?php the_title(); ?>"/>
</a>
</div>
<div class="info"><p><?php the_title(); ?><br/>
<span>
<?php $wp_product = new WC_Product( get_the_ID() );
echo get_woocommerce_currency_symbol(); ?></sup><?php
echo $wp_product->get_price();?></span></p></div>
</div>
</div>
<?php endwhile; ?>
I want grouped product highest price to show in place of $wp_product->get_price();
I tried to get children products and but the array is empty.
foreach ( $wp_product->get_children() as $child_id ) {
$child_prices[] = get_post_meta( $child_id, '_price', true );
}
can anyone suggest me how i can get highest price.
Thanks in advance.
I found the solution.. I tried the same solution earlier but that time it wasn't working.
Here is the fix:
$series_term = $_GET['series'];
$args=array(
'series' => $term_name->slug,
'post_type' => 'product',
'posts_per_page' => 8,
'filter' =>$valuecol,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'hidden' ), // Don't display products in the knives category on the shop page
'operator' => 'NOT IN'
)
)
);
// $do_not_duplicate[] = $post->ID;
$my_query = null;
$my_query = new WP_Query($args);
/* Start the Loop */
while ($my_query->have_posts()) : $my_query->the_post();
$feat_prod_img = wp_get_attachment_image_src(get_post_thumbnail_id(), 'post-thumb-single');
$feat_product_img = wp_get_attachment_url( get_post_thumbnail_id($my_query->post->ID) );
?>
<div class="col-md-3 col-sm-4 col-xs-6">
<div class="product-detail">
<div class="product-thmbnial">
<a href="<?php the_permalink(); ?>">
<img src="<?php $src=$feat_product_img; echo image_product_resize_crop( $src, $w=440, $h=275, $dest = null, $override = false, $createNewIfExists = false ); ?>" alt="<?php the_title(); ?>"/>
</a>
</div>
<div class="info"><p><?php the_title(); ?><br/>
<span>
<?php $wp_product = new WC_Product( get_the_ID() );
$child_prices = array();
foreach ( $product->get_children() as $child_id ) {
$child_prices[] = get_post_meta( $child_id, '_price', true );
}
$child_prices = array_unique( $child_prices );
if ( ! empty( $child_prices ) ) {
$min_price = min( $child_prices );
$max_price = max( $child_prices );
} else {
$min_price = '';
$max_price = '';
}
if($min_price == $max_price && !empty($min_price))
{
echo get_woocommerce_currency_symbol(); ?></sup><?php
echo $min_price;
}
elseif(!empty($max_price))
{
echo get_woocommerce_currency_symbol(); ?></sup><?php
echo $max_price;
}
else
{
echo get_woocommerce_currency_symbol(); ?></sup><?php
echo $wp_product->get_price();
}
?></span></p></div>
</div>
</div>
<?php endwhile; ?>

Output latest posts from each child categories of particular parent category

I want to output latest post from each category (child category) that has parent. Parent category id is 54.
For example, if there are 7 child categories under the category 54, the number of output post should be 7 (all latest from each child category). I hope this makes sense.
My current code is below. At this stage, this code outputs only one latest post (of 1 child category) that has the latest under cat id=54. It would be great if you could advise me how to modify this so that I can get more latest post from multiple child categories.
<?php
$categories = get_categories();
foreach ( $categories as $category ) {
$args = array(
'cat' => 54,
'post_type' => 'post',
'posts_per_page' => '1',
);
}
?>
<?php $query = new WP_Query( $args ); ?>
<?php if ($query->have_posts()) : ?>
<div class="container">
<?php while ($query->have_posts()) : $query->the_post(); ?>
<div class="box">
<article>
<p><?php foreach((get_the_category()) as $childcat) { if (cat_is_ancestor_of(54, $childcat)) { echo ''; echo $childcat->cat_name . ''; }} ?></p>
<?php if ( has_post_thumbnail() ): ?><?php the_post_thumbnail('box-pic'); ?><?php endif; ?>
<h3><?php the_title();?></h3>
</article>
</div>
<?php endwhile;?>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
Here is the logic you need to use,
$term_id = 54;
$taxonomy_name = 'category';
$term_children = get_term_children( $term_id, $taxonomy_name );
echo '<ul>';
foreach ( $term_children as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
$args = array(
'cat' => $term->term_id,
'post_type' => 'post',
'posts_per_page' => '1',
);
$query = new WP_Query( $args ); ?>
<?php if ($query->have_posts()) : ?>
<div class="container">
<?php while ($query->have_posts()) : $query->the_post(); ?>
<div class="box">
<article>
<p><?php foreach((get_the_category()) as $childcat) { if (cat_is_ancestor_of(54, $childcat)) { echo ''; echo $childcat->cat_name . ''; }} ?></p>
<?php if ( has_post_thumbnail() ): ?><?php the_post_thumbnail('box-pic'); ?><?php endif; ?>
<h3><?php the_title();?>
</h3>
</article>
</div>
<?php endwhile;?>
</div>
<?php endif; ?>
<?php wp_reset_query();
}
echo '</ul>';
Try this one,
$term_id =54 ;
$term_children = get_term_children( $term_id, $taxonomy );
$term_id = array();
foreach ( $term_children as $child ) {
$term = get_term_by( 'id', $child, $taxonomy );
$term_id[] = $term->term_id; //childern ids array.
}
Pass the $term_id to tax_query.
$args = array(
'post_type' => $post_type,
'posts_per_page' => 7,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $term_id
)
)
);
$query = new WP_Query($args);
This will work for you.

Shortcode rendering as text not as shortcode should

I am building a shopping website and I am trying to put a shortcode in that will show the customer a buy button and the quantity of the product the customer wants to purchase. On my post page the shortcode works fine:
http://warringah-plastics.com.au/blog/dt_catalog/recess-gasket-large/
but on the archive page:
http://warringah-plastics.com.au/store/
the shortcode id displayed as text and not the actual button and quantity e.g. [add_to_cart item="FPROWAR-160713-1" showprice="no" quantity="user:1" ajax="yes" ].
The code that works in the post page is this:
<?php
$my_textbox_value = mtbxr_val("shopping_shortcode");
echo do_shortcode("$my_textbox_value");
?>
but it just displays the shortcode text on that archive page. Anyone have any ideas? Much appreciated,
UPDATE
THIS IS THE CODE THAT DISPLAYS THE SHORTCODE CORRECTLY:
<?php get_header(); ?>
<?php dt_storage('have_sidebar', true); ?>
<?php get_template_part('top-bg'); ?>
<?php get_template_part('parallax'); ?>
<div id="wrapper">
<?php get_template_part('nav'); ?>
<div id="container">
<?php if( have_posts() ): while( have_posts() ): the_post(); ?>
<h1><?php the_title(); ?></h1>
<h1 style="color: #3C3C3B !important; margin-top:-20px !important;"><?php $terms_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'dt_catalog_category', '', ', ', '' ) );
echo $terms_as_text; ?></h1>
<?php
global $post;
$post_opts = get_post_meta($post->ID, '_dt_catalog-post_options', true);
if( !isset($post_opts['hide_media']) || (isset($post_opts['hide_media']) && !$post_opts['hide_media']) ) {
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC'
);
if( !empty($post_opts['hide_thumbnail']) )
$args['post__not_in'] = array( get_post_thumbnail_id() );
$dt_tmp_query = new WP_Query( $args );
if( $dt_tmp_query->have_posts() ) {
$slides = array();
foreach( $dt_tmp_query->posts as $slide ) {
$video = get_post_meta( $slide->ID, '_dt_catalog_video_link', true );
$tmp_arr = array();
$tmp_arr['caption'] = $slide->post_excerpt;
if ( ! $video ) {
$slide_src = dt_get_resized_img( wp_get_attachment_image_src( $slide->ID, 'full' ), array( 'w' => 710 ) );
$tmp_arr['alt'] = get_post_meta( $slide->ID, '_wp_attachment_image_alt', true );
$tmp_arr['src'] = $slide_src[0];
$tmp_arr['size_str'] = $slide_src[3];
} else {
$tmp_arr['is_video'] = true;
$tmp_arr['src'] = $video;
$tmp_arr['size_str'] = array( 710, 1024 );
}
$slides[] = $tmp_arr;
}
dt_get_anything_slider( array( 'id' => 'slider2', 'items_arr' => $slides ) );
}
}
?>
<?php $opts = get_post_meta($post->ID, '_dt_catalog-goods_options', true); ?>
<?php if( !empty($opts['price']) ): ?>
<span class="price"><?php _e('Price: ', LANGUAGE_ZONE); echo esc_html($opts['price']); ?></span>
<?php endif; ?>
<?php
$my_textbox_value = mtbxr_val("shopping_shortcode");
echo do_shortcode("$my_textbox_value");
?>
<?php
the_content();
if( dt_is_page_soc_buttons_enabled('catalog') ) {
dt_get_like_buttons( get_the_ID() );
}
?>
<?php if( !empty($opts['p_link']) ): ?>
<span><i class="dol"></i><?php _e('Make purchase!', LANGUAGE_ZONE); ?></span>
<?php endif; ?>
<p class="gap"></p>
<?php
$rel_works = get_post_meta($post->ID, '_dt_catalog_related', true);
if( isset($rel_works['show_related']) && $rel_works['show_related'] ):
if( 'same' == $rel_works['related'] ) {
$rel_works['related'] = wp_get_post_terms(
$post->ID,
'dt_catalog_category',
array('fields' => 'ids')
);
}
if( !empty($rel_works['related']) ):
?>
<p class="hr hr-narrow gap-small"></p>
<div class="gap"></div>
<div class="full-width w-photo">
<h2><?php _e('Related Items', LANGUAGE_ZONE); ?></h2>
<?php
if( 'same' == $rel_works['related'] ) {
$rel_works['related'] = wp_get_post_terms(
$post->ID,
'dt_catalog_category',
array('fields' => 'ids')
);
}
$dt_tmp_query = new WP_Query( array(
'posts_per_page' => -1,
'post_type' => 'dt_catalog',
'post_status' => 'publish',
'post__not_in' => array($post->ID),
'tax_query' => array( array(
'taxonomy' => 'dt_catalog_category',
'field' => 'id',
'terms' => $rel_works['related'],
'operator' => 'IN'
) )
) );
if( $dt_tmp_query->have_posts() ) {
$thumb_arr = dt_core_get_posts_thumbnails( $dt_tmp_query->posts );
$items = array();
foreach( $dt_tmp_query->posts as $rel_post ) {
$item = array();
$img = dt_get_resized_img(
dt_get_thumb_meta($thumb_arr['thumbs_meta'], 'full', $rel_post->ID),
array('w' => 196, 'h' => 123, 'use_noimage' => true)
);
$item['src'] = $img[0];
$item['size_str'] = $img[2];
$item['post_id'] = $rel_post->ID;
$item['desc'] = apply_filters('get_the_excerpt', $rel_post->post_excerpt);
$item['title'] = apply_filters('the_title', $rel_post->post_title, $rel_post->ID);
$item['alt'] = esc_attr( $item['title'] );
$items[] = $item;
}
$args = array( 'items_arr' => $items, 'id' => '', 'class' => 'list-carousel recent bx', 'ul_class' => 'slider1' );
$args['wrap'] = '<div class="%CLASS% bx">%SLIDER%</div>';
if( ! empty( $rel_works['show_desc'] ) || ! empty( $rel_works['show_title'] ) ) {
$title = '';
if( ! empty( $rel_works['show_title'] ) ) {
$title = '<h3>%TITLE%</h3>';
}
$desc = '';
if( ! empty( $rel_works['show_desc'] ) ) {
$desc = '<p>%DESC%</p>';
}
$args['item_wrap'] = '
<li>
<div class="textwidget">
<div class="textwidget-photo">
<a class="photo" href="%LINK%"><img src="%IMG_SRC%" alt="%ALT%" %IMG_SIZE% /></a>
</div>
<div class="widget-info">
<div class="info">
' . $title . $desc . '
</div>
</div>
</div>
</li>
';
}
dt_get_carousel_slider( $args );
}
?>
</div>
<?php endif; endif; ?>
<?php comments_template(); ?>
<?php
endwhile;
endif;
?>
</div>
<?php dt_widget_area('sidebar', null, 'sidebar_4'); ?>
</div>
<?php get_footer(); ?>
AND THIS IS THE CODE THAT DISPLAYS THE SHORTCODE JUST AS TEXT:
<?php
global $post;
$page_data = dt_storage( 'page_data' );
$page_opts = ! empty( $page_data['page_options'] ) ? $page_data['page_options'] : array();
$add_data = dt_storage( 'add_data' );
$first_class = '';
if( 1 === dt_storage('post_is_first') ) {
$first_class = ' first';
dt_storage( 'post_is_first', -1 );
}
$opts = get_post_meta($post->ID, '_dt_catalog-goods_options', true);
?>
<div class="<?php dt_portfolio_classes( '2_col-list', 'block' ); echo $first_class; ?>">
<?php
$h = 220;
if ( ! empty ( $page_opts['thumb_height'] ) ) {
$h = $page_opts['thumb_height'];
}
dt_get_thumb_img( array(
'class' => 'photo',
'use_noimage' => true,
'href' => get_permalink(),
'thumb_opts' => array( 'w' => 343, 'h' => $h )
),
'<div class="textwidget-photo">
<a %HREF% %CLASS% %TITLE% %CUSTOM%><img %ALT% %SRC% %IMG_CLASS% %SIZE% /></a>
</div>'
);
?>
<div class="<?php dt_portfolio_classes( '2_col-list', 'info' ); ?>">
<a class="<?php dt_portfolio_classes( '2_col-list', 'head' ); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php if( !empty($opts['price']) ): ?>
<span class="price"><?php _e('Price: ', LANGUAGE_ZONE); echo esc_html($opts['price']); ?></span>
<?php endif; ?>
<?php
dt_the_content();
dt_details_link();
dt_edit_link();
?>
<div id="specialpriceshortcode">
<?php
$my_textbox_value = mtbxr_val("shopping_shortcode");
echo do_shortcode("$my_textbox_value");
?>
</div>
</div>
</div>
Try using single quotes in the do_shortcode call, like so:
echo do_shortcode('$my_textbox_value');
More likely though is that the shortcode isn't defined on the archive page so you'd need to look at where it is being instantiated to see if that is the issue. Normally when a shortcode just echoes out the content it means that shortcode doesn't exist. You can test easily enough by using the shortcode_exists() function:
<?php if ( shortcode_exists( 'add_to_cart' ) ) { echo "The shortcode exists";} ?>
If that doesn't work then you know the issue is with the shortcode not being registered on your archives page. If it does work then you know it's something with the format of the content being passed to the shortcode.
Add this to your functions.php
// Allow shortcodes on widgets
add_filter('widget_text','do_shortcode');
// Allow shortcodes on pages (not tested, but should work)
add_filter('the_content','do_shortcode');
Typically your shortcode is getting registered in a plugin or your theme's functions.php file. In a plugin it's often something like:
add_action('init', 'register_my_shortcode');
function register_my_shortcode(){
add_shortcode('my_shortcode', 'do_my_shortcode');
}
And then you'd have a function do_my_short_code() that actually outputs the content. With something like that the shortcode is getting registered via the 'init' hook (http://codex.wordpress.org/Plugin_API/Action_Reference) which is called before WP has started figuring out what template to use, what content to output, etc.
But some plugins will register the shortcode in a way that it is only available on pages / posts where it's going to potentially be used. For example, I can think of one plugin where they register the shortcode and enqueue some javascripts in the same function. That function checks to see if you're on a particular page before it executes so that the js files are not included unnecessarily all over the place. Since the shortcode registration takes place in the same function it means the shortcode only exists on those pages.
Anyhow, if the shortcode is showing as existing on your archives page you know that isn't the problem, so check that first and let me know what you find.

Single template for custom post type don't show single post in WordPress

I have a problem with my WordPress code in a single-mycustompost.php file.
This is my code:
<div id="contenu">
<?php global $post;?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post();?>
<div id="main-inner">
<?php
$terms = get_the_terms( $post->ID, 'projet');
?> <div id="img-slide">
<div id="img-slide-large">
<?php
$args = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null,
'orderby' => 'menu_order',
'numberposts' => -1,
);
$attachments = get_posts( $args );
if ($attachments) {
foreach ($attachments as $attachment) {
$attachment_url = wp_get_attachment_url( $attachment->ID , 'full' );
$image = aq_resize( $attachment_url, 758, 398, true ); //resize & retain image proportions (soft crop)
echo '<img src="' . $image . '"/>';
}
}
?>
</div>
<div id="img-slide-thumb">
<ul id="nav-thumb">
<?php
if ($attachments) {
foreach ($attachments as $attachment) {
$attachment_url = wp_get_attachment_url( $attachment->ID , 'full' );
$image = aq_resize( $attachment_url, 129, 85, true); //resize & retain image proportions (soft crop)
?>
<li><?php echo '<img src="' . $image . '"/>'; ?> </li>
<?php
}
} ?>
</ul>
</div>
</div>
<div id="infos">
<?php echo esc_html( get_post_meta( get_the_ID(), 'date_create_post_type', true ) ); ?>
<h2><?php the_title() ?></h2>
<h3><?php echo esc_html( get_post_meta( get_the_ID(), 'soustitre_create_post_type', true ) ); ?></h3>
<p><?php the_content(); ?></p>
</div>
<?php endwhile;
endif;
?>
</div>
But it displays the entire list of my customs posts of PROJET. I don't know why it does this; could you help me?
Problem solved !
I'hve forgot that i have a query in the header without
soooo i added in the header and it solved the probleme.
!

How to push a page's custom field data as a value within a template file?

Long story short. I'm trying to expand the functionality of a portfolio template. It currently can only handle one page. I can duplicate the template, relying on custom fields to pull in specific portfilio items (the theme uses custom post types).
So, here's the bit from my portfolio-template.php:
$args = array(
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_value' => 'ao',
'posts_per_page' => -1
);
So, I want to create a new page, select the portfolio template, then define a custom field on that page that will pass on the meta_value to the portfolio-template.php
So it would look like this instead:
$args = array(
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_value' => 'PULL_VALUE_FROM_CUSTOM_FIELD',
'posts_per_page' => -1
);
With "PULL_VALUE_FROM_CUSTOM_FIELD" being the bit that I can't figure out. Sorry if my vernacular is code_crude, I'm new at this!
EDIT: Adding in the full code of the template
<?php
$args = array(
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_value' => 'ao',
'posts_per_page' => -1
);
$portfolio_query = new WP_Query($args);
if( $portfolio_query->have_posts() ) :
echo '<div id="primary" class="hfeed">';
while( $portfolio_query->have_posts() ) : $portfolio_query->the_post();
// styles
$style = 'zilla-' . get_post_meta($post->ID, '_zilla_portfolio_style', true);
$media_pos = 'zilla-' . get_post_meta($post->ID, '_zilla_portfolio_media_position', true);
$width = ( $media_pos == 'zilla-media-center' ) ? 940 : 600;
// project url
$portfolio_url = get_post_meta($post->ID, '_zilla_portfolio_project_url', true);
if( !empty($portfolio_url) )
$portfolio_button_copy = get_post_meta($post->ID, '_zilla_portfolio_project_url_copy', true);
// determine which media to display
$portfolio_display_gallery = get_post_meta($post->ID, '_zilla_portfolio_display_gallery', true);
$portfolio_display_video = get_post_meta($post->ID, '_zilla_portfolio_display_video', true);
$portfolio_display_audio = get_post_meta($post->ID, '_zilla_portfolio_display_audio', true);
// grab everything else
$custom_bg = get_post_meta($post->ID, '_zilla_portfolio_display_background', true);
$portfolio_caption = get_post_meta($post->ID, '_zilla_portfolio_caption', true);
?>
<?php zilla_page_before(); ?>
<!--BEGIN .hentry-->
<div <?php post_class( $style . ' ' . $media_pos ) ?> id="post-<?php the_ID(); ?>">
<?php zilla_page_start(); ?>
<div class="hentry-inner">
<!--BEGIN .entry-media -->
<div class="entry-media">
<?php
if( $portfolio_display_gallery == 'on' ) {
$gallery_layout = get_post_meta( $post->ID, '_zilla_gallery_layout', true);
$slideshow = ( $gallery_layout == 'slideshow' ) ? true : false;
$size = ( $media_pos == 'zilla-media-center' ) ? 'portfolio-full' : 'portfolio-index';
zilla_gallery( $post->ID, $size, $slideshow, $slideshow );
}
if( $portfolio_display_video == 'on' ) {
$embed = get_post_meta($post->ID, '_zilla_video_embed_code', true);
if( !empty( $embed ) ) {
echo stripslashes(htmlspecialchars_decode($embed));
} else {
zilla_video( $post->ID, $width );
}
}
if( $portfolio_display_audio == 'on' ) {
zilla_audio( $post->ID, $width );
}
?>
<!--END .entry-media -->
</div>
<!--BEGIN .entry-content -->
<div class="entry-content">
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php if( !empty($portfolio_caption) )
echo "<p class='zilla-caption'>" . stripslashes(htmlspecialchars_decode($portfolio_caption)) . "</p>"; ?>
<?php if( !empty($portfolio_url) && $media_pos == 'zilla-media-center' ) { ?>
<?php echo $portfolio_button_copy; ?>
<?php } ?>
<?php the_content(); ?>
<?php wp_link_pages(array('before' => '<p><strong>'.__('Pages:', 'zilla').'</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
<?php if( !empty($portfolio_url) && ( $media_pos == 'zilla-media-left' || $media_pos == 'zilla-media-right' ) ) { ?>
<?php echo $portfolio_button_copy; ?>
<?php } ?>
<!--END .entry-content -->
</div>
</div>
<?php zilla_page_end(); ?>
<!--END .hentry-->
</div>
<?php zilla_page_after(); ?>
<?php endwhile; ?>
<!--END #primary .hfeed-->
</div>
<?php else: ?>
<div id="content">
<!--BEGIN #post-0-->
<div id="post-0" <?php post_class(); ?>>
<h2 class="entry-title"><?php _e('Error: No Portfolios Found', 'zilla') ?></h2>
<!--BEGIN .entry-content-->
<div class="entry-content">
<p><?php _e("Sorry, but no portfolios have been created.", "zilla") ?></p>
<!--END .entry-content-->
</div>
<!--END #post-0-->
</div>
</div>
<?php endif; ?>
Before the Loop, you can access the global variable $post, which in case of single.php and page.php will represent that very post type (post/page/cpt) object that will be displayed by the template file.
If not mistaken, when used in category.php, for example, it will grab the first one.
So, in the case in question:
<?php
global $post;
$the_meta = get_post_meta( 'custom_field_name', $post->ID );
if( !$the_meta )
$the_meta = 'your_backup_solution';
$args = array(
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_value' => $the_meta,
'posts_per_page' => -1
);
$portfolio_query = new WP_Query($args);
// et cetera

Resources