WordPress Custom Fields get post from page - wordpress

I'm working on a WordPress website and i have a little problem with the custom fields plugin. I'm familiar with plugin and worked with it few times before.
I created a custom field for posts and a custom field for pages.
When i try to get the page's custom field i get it with no problem.
The page has global $post on top and i do get a different $post->ID
for each post loaded.
$pageMonth = get_field('page-month');
But when i try to get the post's field i get nothing.. I also tried it through
the post's loop with $post->ID
<?php $postMonth = get_field('post-month', $post->ID); ?>
I checked myself few times, the custom field names are fine and i did
attached the post and the page to their custom fields and everything..

Related

Show custom post archive when custom post not specified

I have a custom post type called produce set up in WordPress and a custom taxonomy called produce_category.
The URL format for posts in this custom post type is in this format http://mywebsite/produce/%produce_category%/%postname%/. Of course %produce_category% and %postname% get substituted accordingly, a working example url would be http://mywebsite/produce/fruits-and-vegetables/avocado.
What I would like to do is show the produce_category custom taxonomy archive if a user visits http://mywebsite/produce/%produce_category%, without specifying post name at the end, e.g http://mywebsite/produce/fruits-and-vegetables to show all produce in fruits-and-vegetables produce_category.
Besides that when a user visits http://mywebsite/produce/ I would like to show all the produce archive. EDIT: I have this working now.
I know how to create the archive pages totally fine and have no problem with that. I am stuck at creating permalinks. When I visit http://mywebsite/produce/%produce_category% I get a 404 error.
I'm looking for advise on the best way to implement this. Currently I am using Custom Post Type Permalinks and CPTUI.
The CPTUI custom taxonomy settings interface does not allow me to have a blank in the custom rewrite slug. It defaults to the custom taxonomy slug, produce_category, when I don't fill in anything.
This gives the front-side produce_category taxonomy archive url as http://mywebsite/produce/produce_category/%produce_category%/ e.g. http://mywebsite/produce/produce_category/fish-and-seafood/ when what I want for the archive is http://mywebsite/produce/fish-and-seafood/.
Please help with suggestion on the best way I can achieve the custom taxonomy url.
Thank you all.
Try this code. It will help you to achieve your url structure... Make sure you update permalinks after saving it to functions.php
function custom_produce_category_link( $link, $term, $taxonomy )
{
if ( $taxonomy !== 'produce_category' )
return $link;
return str_replace( 'produce_category/', '', $link );
}
add_filter( 'term_link', 'custom_produce_category_link', 10, 3 );

Wordpress CPT UI and ACF custom fields

i am using CPT UI for custom post type and ACF custom fields.
i have created custom post type (companies) and linked to its custom fields. In admin everything is okay. I want to display this in front end. The user can be enter the company details also. How can i display this in front end ?
<?php
$id = get_the_ID();
$data = get_field('custom_field_name', $id);
echo $data;
?>
Obviously this will need a lot of refinement to look good but that's the basics on how to retrieve data from an ACF field. You may need to var_dump($data); in order to figure out how to get the part you want (if it's not just storing a string). Without more details on what you're doing that's a good an answer as I can give you.

woo commerce - how to show custom fields on published page

I have set 2 custom fields on the wordpress / woocommerce product page called "imballo" and "sottoimballo" with their values. How do I display these on the published page?
Thanks!
You would have to display the custom fields in the WooCommerce Single Product Page template,
You could do it in two ways,
Add a code in an existing hook of the Product Single page. Check hooks here
Modify the template by overriding it in your WordPress theme. Read here
And in the place where you want to display do the following,
<?php
//Please add your HTML structure as needed.
echo get_post_meta(get_the_ID(), 'imballo');
echo get_post_meta(get_the_ID(), 'sottoimballo');
?>

Display Custom Post Type within a page template in Wordpress

I am using custom post type plugin to create custom post types for my Wordpress website.
I want to add the custom post types to a page template. but, when I add the php loop to call the custom post type that I created, it calls the page content instead.
Does anyone know how to solve this problem? Thanks.
Try this before the loop:
<?php query_posts(array('post_type' => 'your-cpt-name') ); ?>

Wordpress custom fields content don't appear when searched

I'm using the plugin Advanced Custom Fields on a custom post type. When I enter in content into the custom fields and publish, all looks fine BUT when trying to search for words that are in a custom field it doesn't show up...
I really need this content to be searchable.
I'm displaying the custom fields using this code
<?php echo get_post_meta($post->ID, 'course-code', true); ?>
Can anyone help please.
Post meta is not included in WordPress default search. You could use a plugin like Relevanssi, which extends WP's search functionality.

Resources