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

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

Related

How to display excerpt for custom post type

I need help with custom post type excerpt. I managed to add to function.php to get the excerpt showing on the edit page. Now I can find how to display it on the taxonomy.php file Need to replace that:(Which shows post content)
<p><?php echo tfuse_get_short_text($holiday['post_content'],10); ?></p>
By something to show post excerpt.
Try this code. It may be helpful for you.
$args = array( 'post_type' => 'custom-post-typename' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_excerpt();
endwhile;
If you already have POST object or post_id, you can do like this where you need:
echo get_the_excerpt( post object or post ID );
or if you in a loop, you can use get_the_excerpt() function without any variables.
You can read more about this here: https://developer.wordpress.org/reference/functions/get_the_excerpt/ .
I hope it's helpful for you.

how to show the data of CPT UI plugin on website?

I have used CPT UI to add some posts with taxonomies. I have filled two post data in CPT UI for practice. Now I want to show these post on a page. What all code I have to write.
You can use Wp_Query along with the post name that you created using CPT Ui plugin to display those posts. Like, e.g. i had created a post named as school then code to display all posts of School type is as following :
$query = new WP_Query( array( 'post_type' => 'school' ) );
while($query->have_posts()):
$query->the_post();
echo $query->ID; // it will print the ID of post
endwhile;
Hope this will clear the things..
In order to pull in the custom fields/post meta, you will need to write some code within the WordPress loop (https://codex.wordpress.org/The_Loop) in your template file.
The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags. Any HTML or PHP code in the Loop will be processed on each post.
eg.
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
For all post meta:
$meta = get_post_meta( get_the_ID() );
echo '<pre>';
print_r( $meta );
echo '</pre>';
Or for a single value:
$custom_field_value = get_post_meta( get_the_ID(), 'custom_field_key_name', true );
See below for more information in the WordPress Codex:
https://codex.wordpress.org/Custom_Fields
and
https://developer.wordpress.org/reference/functions/get_post_meta/

Print WooCommerce products list sorted by SKU

My client has asked "I would like to be able to print a listing of all products by SKU (sorted alphanumerically) with description, and by description (alpha) with the associated SKU." I think the closest I could get for him is to have the SKU sorted alphanumerically but not both, though I could be wrong.
Looking at this question: Woocommerce: get a list for all sku product, I wonder if I could build a table instead of a list and pull the description in next to the SKU?
Here's the code I'm thinking of based on the link above (without sorting):
$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
query_posts( $args );
if( have_posts() ):
echo '<table>';
while ( have_posts() ) : the_post();
echo '<tr><td>'. $product->get_sku() . ' - ' . $product->get_title() . '</td><td>' . $product->get_description() . '</td></tr>';
endwhile;
echo '</table>';
endif;
I don't think the description is the correct way to call it, but I didn't see one to call in the short description.Also, I'm not sure how I would have it sort by SKU. My PHP knowledge is limited.
Even if that code worked, I'm not sure where I would put it to call this in. Can I make it on an admin page or would it be better to create a regular page and use a different template and put this directly into the PHP of that template file?
Any suggestions would be great!

wordpress query (shortcode) returns always the first post

I have made a shortcode inside my plugin, which is working great .
The shortcode needs to take some parameters and create a custom loop with output.
One of the parameters is how many posts to output the loop for ($markers)
$args=array(
'meta_key'=>'_mykey',
'post_status'=>'publish',
'post_type'=>'post',
'orderby'=>'date',
'order'=>'DESC',
'posts_per_page'=>$markers,
);
$wp_query = new WP_Query();
$wp_query->query($args);
if ($wp_query->have_posts()) : while (($wp_query->have_posts()) ) : $wp_query->the_post();
// do the loop using get_the_id() and $post->id
endwhile;endif;
wp_reset_query();//END query
On occations I will need to have data from ALL posts ($markers = '-1' ) and sometimes only one ($markers = '1' ) or muliple ($markers = 'x').
All of those work great on single pages / posts - but My problem is that when this function is in a place where I have more than one post (!is_single) and ($ markers = '1' )it will always return the data for the LATEST post , and not for the correct one ..
(for example in the default wordpress theme, where it would display10 posts - they will all be the same data )
It is obviously a problem of the $post->ID - but how can I have the correct post ID when doing a custom loop OUTSIDE the wp loop ?
I tried to ovverride the problem by
global $post;
$thePostIDtmp = $post->ID; //get the ID before starting new query as temp id
$wp_query = new WP_Query();
$wp_query->query($args);
// Start Custom Loop
if (!is_single()){
$post_id_t = $thePostIDtmp;}
else {
$post_id_t = $post->ID;}
and then use $post_id_t - but it did not seems to work ,
Should I not use get_the_id() ? or should I not use query (and use get_posts) ??
Any ideas / solutions / thoughts ??
I would use query_posts(http://codex.wordpress.org/Function_Reference/query_posts)rather than override the $wp object. You should be able to include as many loops on the page as you want with this. If you have problems with this you can use: http://codex.wordpress.org/Function_Reference/wp_reset_query just before you call it.
I find this: http://blog.cloudfour.com/wordpress-taking-the-hack-out-of-multiple-custom-loops/
takes a bit of the pain away too.
There are basically two sorts of querying posts in WordPress: Those that alter the main loop and those that do not. If you want to change the main loop like the one used to display category archive pages then use query_posts. It let's you do exactly that. Delete, change and append parameters of the default query to change the outcome of a typical page.
query_posts has some drawbacks though.
Then there are queries that are just used to get stuff out of the database to play around with e.g. displaying the latest post titles in the sidebar or the attachments of the current post.
To do that create a new WP_Query object that will build your custom loop independently of the main loop like so:
// The Query
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
Then there is get_posts() which is like the little brother of WP_Query. It has an easier interface in my opinion and returns an array with the results that is easier to work with.
It looks like this:
$myposts = get_posts( $args );
foreach($myposts as $post) : setup_postdata($post);
echo "<li>";
the_title();
echo "</li>";
endforeach;
Inside the foreach template tags like get_the_id() will work.

How do I list all Entries with a certain tag in Wordpress?

I may just be missing this functionality, but does anyone know if there is a widget available:
I need to list the subject for all the entries that are associated with a given tag.
For example: I have 5 articles tagged with "Tutorial", I'd like to see a list as follows:
Tutorial 1: Installing the app
Tutorial 2: Customizing
Tutorial 3: Advanced edits
Tutorial 4: User managment
Does functionality like this exists in wordpress allready?
If you are comfortable with hacking WP you can try adding to your sidebar with wp_list_pages, http://codex.wordpress.org/Template_Tags/wp_list_pages.
Or there are plug-ins like Simple-Tags(http://wordpress.org/extend/plugins/simple-tags/) that help you manage your tags.
The nice thing about WordPress is there are lots of plug-ins available that can add functionality that the base app does not ahve, a quick search for plug-ins for tabs(http://wordpress.org/extend/plugins/search.php?q=tag) returned quite a list, sure it's a lot to dig through but that also helps you see what is available.
So i found an article on using custom queries. I modified the script to pull a specific tag, in this case "Open Source".
<?php
$querystr = "SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->terms wterms, $wpdb->term_relationships wterm_relationships, $wpdb->term_taxonomy wterm_taxonomy
WHERE wterm_relationships.object_id = wposts.ID
AND wterm_relationships.term_taxonomy_id = wterm_taxonomy.term_taxonomy_id
AND wterms.term_id = wterm_taxonomy.term_id
AND wterm_taxonomy.taxonomy = 'post_tag'
AND wterms.name = 'Open Source'
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'
ORDER BY wposts.post_date DESC";
$pageposts = $wpdb->get_results($querystr, OBJECT);
?>
<?php if ($pageposts): ?>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); ?>
<?php the_title('<li>', '</li>'); ?>
<?php endforeach; ?>
<?php else : ?>
<?php endif; ?>
If you only want to list pages for one specific tag then this would work. However, say you wanted to give a listing of pages for each tag based on the current articles listed on the page.
You might create an array of all the tags using the get_the_tags() function during The Loop and then use that array to dynamically generate the WHERE statement for the query.
You can easily use get_posts to create an array of posts based on a set of parameters. It retrieves a list of recent posts or posts matching these criteria.
In your case, I would like to show how to display your posts under a specific tag ( in your case, Tutorial ) by creating a short code, which can be easily used anywhere later on in your site.
In your functions.php
function shortcode_tag_t() {
$uu_id=get_current_user_id();
$args = array(
'posts_per_page' => 10,
'tag' => 'Tutorial',
'post_type' => 'post',
'post_status' => 'publish'
);
$posts_array = get_posts( $args );
foreach ( $posts_array as $post ) : setup_postdata( $post );
$url = $post->guid;
echo"<li><a href='".$url."'>" .$post->post_title."</a></li>";
endforeach;
wp_reset_postdata();
}
add_shortcode('your_shortcode_name', shortcode_tag_t );
Now you have a list of 10 posts tagged under Tutorial.
Echo the created short code where ever you want to display the list.

Resources