Skip to main content
replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
Source Link

This is my loop (based on this postthis post):

This is my loop (based on this post):

This is my loop (based on this post):

added 140 characters in body
Source Link
Pieter Goosen
  • 55.4k
  • 23
  • 115
  • 209
<?php add_shortcode( 'current-userposts' , 'show_userposts' );
function show_userposts(){
$user_id = get_current_user_id();
$loop = new WP_Query( array( 'post_type' => 'photo-spot', 'author' => $user_id ) ); 
if ( $loop->have_posts() ) {    
?>
<form class="pps-results"><?php
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
                <fieldset>
                    <legend class="title-block">
                        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                    </legend>
                    <div class="category-block">
                        <?php $categories = get_the_category(); 
                        if ( ! empty( $categories ) ) {
                            echo esc_html( $categories[0]->name );   
                        }
                        ?>
                    </div>
                    <?php
                    if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
                        ?><div class="image-block"><?php
                    the_post_thumbnail( 'thumbnail' );
                        ?></div><?php
                    } 
                    ?>
                    <div class="address-block">
                        <?php echo get_post_meta($post->ID, '_cf_address', true); ?>
                    </div>
                    </div>
                </fieldset>
            <div class="clear"></div>     
<?php endwhile; ?>
        </form>
        <?php
}
wp_reset_postdata();
} ?>

function show_userposts(){ $user_id = get_current_user_id(); $loop = new WP_Query( array( 'post_type' => 'photo-spot', 'author' => $user_id ) ); if ( $loop->have_posts() ) {
?>

have_posts()): $loop->the_post();?>" title="">name);}?>ID, '_cf_address', true);?>
<?php add_shortcode( 'current-userposts' , 'show_userposts' );

function show_userposts(){ $user_id = get_current_user_id(); $loop = new WP_Query( array( 'post_type' => 'photo-spot', 'author' => $user_id ) ); if ( $loop->have_posts() ) {
?>

have_posts()): $loop->the_post();?>" title="">name);}?>ID, '_cf_address', true);?>
<?php add_shortcode( 'current-userposts' , 'show_userposts' );
function show_userposts(){
$user_id = get_current_user_id();
$loop = new WP_Query( array( 'post_type' => 'photo-spot', 'author' => $user_id ) ); 
if ( $loop->have_posts() ) {    
?>
<form class="pps-results"><?php
        while ( $loop->have_posts() ) : $loop->the_post(); ?>
                <fieldset>
                    <legend class="title-block">
                        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                    </legend>
                    <div class="category-block">
                        <?php $categories = get_the_category(); 
                        if ( ! empty( $categories ) ) {
                            echo esc_html( $categories[0]->name );   
                        }
                        ?>
                    </div>
                    <?php
                    if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
                        ?><div class="image-block"><?php
                    the_post_thumbnail( 'thumbnail' );
                        ?></div><?php
                    } 
                    ?>
                    <div class="address-block">
                        <?php echo get_post_meta($post->ID, '_cf_address', true); ?>
                    </div>
                    </div>
                </fieldset>
            <div class="clear"></div>     
<?php endwhile; ?>
        </form>
        <?php
}
wp_reset_postdata();
} ?>
Source Link

Meta Key Value in current-user-only loop

I created a loop for logged-in-users-only which works perfectly for all data except for meta keys. I'd like to be able to also extract meta keys but had no luck so far.

This is my loop (based on this post):

<?php add_shortcode( 'current-userposts' , 'show_userposts' );

function show_userposts(){ $user_id = get_current_user_id(); $loop = new WP_Query( array( 'post_type' => 'photo-spot', 'author' => $user_id ) ); if ( $loop->have_posts() ) {
?>

have_posts() ) : $loop->the_post(); ?> " title=""> name ); } ?> ID, '_cf_address', true); ?>

The function is called via a shortcode

When I use the exact same loop in a search.php template the custom value ('_cf_address') shows up perfectly but somehow not with the shortcode...

Any idea what might be going wrong?

PS> The preview screen doesn't show my html code - I hope it show up alright.