Display more than one Custom Field for a post - wordpress

I have two "Custom Fields" assigned to a post I have. Both of these "Custom Fields" have the same name, but different "Value". At the moment, my code below only presents one of the links. I am trying to get it to display both. So anytime I add another "Custom Field" with the name of "Featured-Blog", it will continue display all of them.
Custom Field's
1) Name: Featured-Blog and Value: 704 (704 is the postID)
2) Name: Featured-Blog and Value: 699 (699 is the postID)
Code being used to display a link to each of the posts. (can only get one of the custom fields to display)
Screenshot of output
Code being used
<?php $related = get_post_meta($post->ID, "Featured-Blog", $single=true);
$related=explode(',',$related);
$args = array_merge( array('post__in' => $related, $wp_query->query ) );
query_posts($args);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="<?php the_ID(); ?>">
<p class="caption"><?php the_title(); ?></p>
</div>
<?php endwhile; else: ?>
<p>no related</p>
<?php endif; wp_reset_query();?>
Now below is an older code that I was originally trying to use, but didn't end up using. This one actually does pull both of my "Custom-Fields". You can see it's obviously coded different because you can see it says "Title" instead of the posts title. But I am just using this code as an example to show you that more than one "Custom Field" can be displayed, unless there is an easy fix for the code below?. Maybe some of the code form that can be incorporated into my working script above. Both the above code, and this bottom one are very close to what I'm trying to do. It seems like one, has something the other one needs.
Screenshot of output
<div id="related-posts">
<?php
$custom_fields = get_post_custom($post_id); //Current post id
$my_custom_field = $custom_fields['Featured-Blog']; //key name
foreach ( $my_custom_field as $key => $url )
echo $key ="<a href='".$url."'>TEST</a><br /><br /><br/>";
?>

You simply have to pass false instead of true when using get_post_meta():
$related = get_post_meta( $post->ID, "Featured-Blog", false );
var_dump( $related );
With the var_dump you'll be able to see the raw contents of the variable. Anyway, you will receive an array, so you can simply do:
$related = get_post_meta( $post->ID, "Featured-Blog", false );
$args = array_merge( array('post__in' => $related, $wp_query->query ) );
get_post_custom, on the other hand, grabs ALL custom fields of a post and produces the same result at the end, it just takes an extra command for you to the the values.
Note that you should not be using query_posts.

Related

How to list posts from categories with custom post type? (WP)

Hi I am having an issues with custom post type.
So what I am trying to do, list all posts from subcategories while calling a main category. Usualy this worked with no problem with normal post type from Wordpress, but since I tried to use custom post type it's not working...
My category structure is like this:
Category
Sub category
( Posts inside )
Sub category
( Posts inside )
Any help or tips are appreciated. Thanks
<?php
$categories = get_categories('title_li=&hide_empty=1&parent=1430');
foreach($categories as $category) {
echo "<div class='col-12' style='border-bottom: 0'><h1 class=''>".$category->name."</h1></div>";
$args = array('cat'=> $category->term_id);
if (have_posts() ) : while (have_posts() ) : the_post(); ?>
<!-- article -->
<article class="col-3">
<div class="image">
<span class="helper"></span><?php the_post_thumbnail('full');?>
</div>
<h1><?php the_title(); ?></h1>
<?php the_content();?>
</article>
<!-- /article -->
<?php endwhile; endif; }?>
</main>
There are a couple of problems going on here:
First, you're not declaring a loop, or calling get_posts.
Second, if you check out the documentation for WP_Query (which is the "backbone" behind get_posts, so the arguments are essentially the same), you'll see that if you do NOT pass in an argument for post type, the default is post.
So, since you've not shared with us the post type, you'll have to adjust the below as needed:
// .. your code above ....
$args = array(
'cat'=> $category->term_id,
// Include the post_type in the query arguments
'post_type' => 'custom-post-type' // Change this as needed
);
// Now we need to actually query for the posts...
$custom_posts = new WP_Query( $args );
// These are modified to use our custom loop...
if ($custom_posts->have_posts() ) : while ($custom_posts->have_posts() ) : $custom_posts->the_post(); ?>
// .. your code below ... the_title(), etc will work here...

shortcode passes custom post type name to pagetemplate

i have a page template in place assigned to a page named cartoonbooks. the custom post type is also called cartoonbooks. how do i get the page to pass the customposttype to the pagetemplate so that i can use the same template for different pages. i tried using shortcodes but unsuccessful ...plz help
functions.php
<?php
function my_shortcode_handler( $atts, $content = null ) {
extract( shortcode_atts( array(
'attr_1' => '',
// ...etc
), $atts ) );
return $attr_1;
}
add_shortcode( 'myshortcode', 'my_shortcode_handler' );
?>
page-books.php
<article>
<?php
//Define the loop based on arguments
$loop = new WP_Query( $attr_1);
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
?>
<?php the_content(); ?>
<?php endwhile;?>
</article>
in the page i put the short code
[myshortcode attr_1="cartoonbooks"]
i updated my code still it doesn't work ...heres the new one
functions.php
function shortcode_handler( $atts, $content = null ) {
extract( shortcode_atts( array(
'posttype' => '',
// ...etc
), $atts ) );
return do_shortcode($posttype);
}
function register_my_shortcode(){
add_shortcode( 'shortcode', 'shortcode_handler' );
}
add_action('init','register_shortcode');
?>
page.php
<?php
$args = do_shortcode($content);
//Define the loop based on arguments
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div><?php the_title('')?></div>
<?php endwhile;?>
in the page called carttonbooks itself i put the code
[myshortcode posttype="cartoonbooks"]
in anothe page called adventurebooks i put code
[myshortcode posttype="adventurebooks"]
but the template doesn't take the posttype from the shortcod and display he content...plzhelp
In wordpress for template file to call shortcode we usedo_shortcode()
try this:
<?php echo do_shortcode('[myshortcode "cartoonbooks"]'); ?>
Know more
Why do any of this. It seems to be a waste.
Check the post type in the template. http://codex.wordpress.org/Function_Reference/get_post_type
if(get_post_type( $post ) == 'cartoonbooks') {
//Your special post type template code
} else {
// Your normal template code
}
ok for those who may have this problem in future...i found the solution by test and trial method...and its very easy.
now say your posttype is books and the slug is books too so the archive page will be archive-books.
create a page with slug as books(slug os archive and page should be same) and add it to menu no need for any template it automatically grabs the archive-books content...so whatever styling u need put it on the archive page

Wordpress: Changing a posts category after a comment is posted

There is plenty of info out there on changing post categories, just nothing relating specifically to changing post categories after a comment is made.
I'd like to have a section of my site showing only posts with comments.
I've tried adding this code I found on this website for changing tags, in about a dozen different places
<?php wp_set_object_terms( '<?php the_ID(); ?>', 'new', 'category', 'true' ); ?>
I've tried using it in
<?php if ( have_comments() ) : ?>
<?php endif; ?>
I've tried using wp_insert_comment and comment_post hooks with other codes around the internet, with no success.
I guess the same thing could be achieved with adding tags to posts after a comment is made but I've no idea where to start there.
Have any of the experts on here ever seen a wordpress blog with a seperate area only showing posts with comments? I'm no coder, I might be attempting the impossible without even being aware of it.
Any help appreciated.
You can use comment_post action hook. This function can't take post ID or $post object as parameters, so you have to add global $post; to get access to it.
From wp-includes/comment.php:
do_action('comment_post', $comment_ID, $commentdata['comment_approved']);
Example usage
//in functions.php
add_action( 'comment_post', 'so_custom_comment_post' );
function so_custom_comment_post(){
global $post;
//Be sure the term 'new' is already available
wp_set_object_terms( $post->ID, 'new', 'category', true );
}
Hope it helps! Let me know if you get stuck.
<?php
$args = array('post_type' => 'post');
$post_obj = new WP_Query($args);
while($post_obj->have_posts() ) : $post_obj->the_post();
//display posts with comments
$comments = get_comments(array(
'post_id' => $post->ID));
foreach($comments as $comment) {
echo '<li>'.the_permalink().'</li>';
}
endwhile;
?>

Advanced custom field (wordpress plugin of same name) not showing

I've set up some fields using the advanced custom fields.
I’ve created a custom field and a post that uses that custom field. I’m trying to display it on a page like this:
<?php
$args = array( 'post_type' => 'Portfolio Item' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<p>' . the_title() . '</p>';
echo '' . the_field('portfolio_url') . '';
endwhile;
?>
The title displays no problem, but the custom field does not i.e. the output is just:
The name ‘portfolio_url’ is the ‘Field Name’.
Can anyone help with what I’m doing wrong?
Maybe you should try and send in smaller snippets of code.
Or give an online example.
Basically if you add a the_field('bottom_quote') function on your page it should echo out the current pages' "bottom_quote" field.
If you're not in a WP loop you have to explicitly point to the post you want to get the field from of using an ID:
<?php the_field( 'bottom_quote', $post->ID );
Also note that $post should either be global or in a foreach loop.
I don't think the post_type parameter is allowed to have a space. Check that you're using the correct slug for that first.
I am not to familiar with this specific plugin but you may need to call in the global $variable that I know when using a class like WPAlchemy you need to call $meta
Check here http://codex.wordpress.org/Function_Reference/get_post_meta

Wordpress Shortcode in custom field within custom post type

Hi Guys Need help with this very badly.
Need to add shortcode to output in the area circled in white in the picture below.
And the input area is under video description. And from my understanding ive have confirmed that the name for that text area is description_value.
I have looked through every documentation and tried all filters and do_shortcode variations to no avail. Please help i have spent 3 days non stop doing this. Puting the codes in my function.php and so many others. It still does not parse [shortcodes] it just displays text "[shortcodes]". please refer to picture below
Thank you.
This is outputing on the page. I have
<div class="describe-feat">[postexpirator]</div>
This is in a file called grid-gallery.js
<h2><%= item.title %></h2></div><div class="view-gallery">\
<div class="describe-feat"><%=item.desc%></div>\
<% if(item.imgnum){ %><span class="item-num"><%= item.imgnum %></span><% } %>\
This is in custom post editor in wordpress admin area
<textarea name="description_value" class="option-textarea">[postexpirator]</textarea>
https://www.dropbox.com/s/almn09e1dwmeywb/shortcodxe.jpg
It's because, you are missing do_shortcode function for parsing shortcode.
Assuming you just want to target a single value, you could just do this inside the loop.
<?php echo ( do_shortcode( get_post_meta( $post->ID , 'Your textarea Key Name' , true ) ) ); ?>
If your post has multiple values for that custom field, then you can set the above to false.. and loop over the array...
<?php $values = do_shortcode( get_post_meta( $post->ID , 'Your textarea Key Name' , false ) ); ?>
<?php if($values && is_array($values)) : ?>
<?php foreach( $values as $meta) : ?>
<p><?php echo $meta ?></p>
<?php endforeach; ?>
<?php endif; ?>
Just want to add that if you won't use the_post() no shortcode will work,
I had this issue when trying to enable shortcode on CutomFields on a new page type, and nothing worked until activating WP loop with the_post() .

Resources