wordpress accessing custom fields from post - php

I have looked everywhere and am not finding the answer I'm looking for.
Basically I want to access information from a post and display it on my front page.
I'm using a plugin called Advanced Custom Fields to organize my content for each post.
I can retrieve the post title like this.
$post_id = 148;
$queried_post = get_post($post_id);
?>
<h2><?php echo $queried_post->post_title; ?></h2>
<?php echo $queried_post->field_4f2af94aa1371; ?>
But I can not access the value of the custom fields. Can anyone help me?

Use the get_post_meta function
<?php echo get_post_meta($post_id, 'CUSTOM_FIELD_NAME', TRUE ); ?>
first param: post ID
second param: key (name of the meta value)
third param: true if you want a single value (instead of array)
For more information; http://codex.wordpress.org/Function_Reference/get_post_meta

Related

WordPress: Get post slug for name of module in Custom Post Type

Got a Custom Post Type called Post Modules or the slug of post-modules. In that I have created using Advanced Custom Fields a number of reusable modules to allow me to insert content in another CPT. Each module is basically a post.
What I want to do is grab the slug of the post within post-modules and display it as a class within my section. So it would be something like:
<section class="post-module-name"></section>
The title of the post would be acceptable as well.
So I came up with the following:
<section class="toolkit-module scroll-section <?php echo $post->post_title; ?> <?php echo $module_type; ?>" data-index="<?php echo $i; ?>" data-module-id="<?php echo $module_id; ?>" data-module-type="<?php echo $module_type; ?>">Content Goes Here</section>
But that pulls the title of the post that displays all these modules and not the module slugs / names themselves.
So if I have a post called "Videos" and I have two modules called let's say music-videos and sports-video, I want to incorporate music-videos in my first section's class and sports-videos in my second.
Is there some way I can pull that post slug coming from the post type and name of the module I'm actually pulling the data out of?
Check out wp_query. You’ll need to use that to access custom post types.
https://developer.wordpress.org/reference/classes/wp_query/
Yes if you already have a WP_Post object you can use it as follows.
echo $post->post_type; // Outputs the actual type of a WP_Post object
echo $post->post_name; // Outputs the slug of the post;
You can take a look at all the properties here, in your specifc case you need something like:
<section class="toolkit-module scroll-section <?php echo $post->post_name; ?> <?php echo $post->post_type; ?>" data-index="<?php echo $i; ?>" data-module-id="<?php echo $post->ID; ?>" data-module-type="<?php echo $post->post_type; ?>">Content Goes Here</section>
It looks like if I use php echo $module_id; I can display the ID number of the module, which at the very least will show something unique. Still I can't locate any documentation anywhere about how to access ACF modules beyond using module_type and module_id.
Found the solution. This did it.
$module_slug = $module->post_name;
then to display it:
<?php echo $module_slug; ?>

Potential Security Issues with Passing Wordpress Variable as a Parameter on get_permalink()

I need to make sure I'm not opening up a security hole in my site. I have a Custom Post Type called "Communities" and a custom post type called "Floor Plans" and need to add a parameter to the URL on Single Floorplan posts.
My goal is that when a Floorplan post object is selected from a Communities Single post, it appends the name of the community (i.e. single community post title) to the URL for use on the following page. So, enabling query vars:
<?php
//* Enable queryvars (functions.php)
add_filter('query_vars', 'parameter_queryvars' );
function parameter_queryvars( $qvars )
{
$qvars[] = "community";
return $qvars;
}
I have a template part called "dynamic-floor-plans" that lives in a loop at the bottom of Single Communities posts. I'm grabbing the single_post_title of the current communities page, and passing that to the URL so that it's available using $_GET
The reason we opted for this method is that the "Floor Plans" post type has a canonical/default version that has the same properties as its Community-specific counterparts. In order to avoid a post relationship situation (which would give the user flat out wrong data in some cases) the community versions just have to be able to use
<?php
//* dynamic-floor-plans template part (placed in loop on communities page)
global $post;
$string = strtolower(single_post_title( null, false ));
$slug = str_replace(' ','-',$string);
?>
<div class="card-button">
<a class="small expanded secondary button"
href="<?php echo get_the_permalink() . '?community=' . $slug ?>">View Floorplan</a>
</div>
Now, this totally is working. I'm able to grab that community variable on the subsequently-selected floorplan single page using $_GET and things seem to be working nicely on the front end:
<?php
//* Single Floorplans Template
$community = $_GET["community"];
?>
<h1 class="entry-title"><?php echo $title ?></h1>
<h4>
<?php if ($community) { ?>
At <?php echo $community; } ?>
</h4>
...
I just can't help but feel that this isn't kosher, or that there's a more secure way to do this.
Am I open to exploitation? I can include the function for the loop itself as well, but figure this code will be sufficient to get started in evaluation.

Echo Post ID from Post Slug or Post Title

I try to get a post ID based on the post tile or slug (doesn't matter to me which one). After that I want to add the ID to a shortcode.
Working code
<?php
$test123 = get_post(30);
echo $test123->ID; /* this works and returns 30 */
?>
<?php
echo do_shortcode("[shortcode id='{$test123->ID}']"); /* this also works */
?>
So the next step is get the post ID based on slug or title. How do I do this? I tested different codes but nothing works till so far.
Many thanks for any help in advance!
To get post by slug use url to postid() function (documentation):
$post_id = url_to_postid( $url );
To get post by title you can use get_page_by_title() function (documentation):
$post = get_page_by_title( 'Your post title', OBJECT, 'post' );
$post_id = $post->ID;
Got the solution
<?php
$homepage1 = url_to_postid('/here-is-my-custom-post-permalink/post-slug/');
echo do_shortcode("[my_shortcode id='{$homepage1}']");
?>
If it is a normal post, you can skip the /here-is-my-custom-post-permalink/ part

wordpress blog index is post page

I have a custom page named 'Journal', which I use as a blog index page for my wordpress website. I've run into a rather strange problem. When I enter <?php echo get_the_title(); ?> or whatever in home.php, it returns the title of a post, instead of the page title 'Journal'. Is anyone familiar with this problem?
Thanks!
This is the expected behavior for this page. When you set a page to be your "blog", you can't access the template tags for that page. Instead, the template tags are for the loop of the posts to be displayed on that page.
To get the title, you have to first get the id of that page, and then you can pass it to a function:
<?php
$page_for_blog = get_option( 'page_for_posts' );
$page_title = get_the_title( $page_for_blog );
?>
Now you can print the $page_title and you should see "Journal".
Updated with Advanced Custom Fields
Now that you have the Journal page's id ($page_for_blog), you can get your field values with:
$field_value = get_field( 'field_name', $page_for_blog );
Obviously, replace 'field_name' with whatever field you're trying to retrieve.

Using Advanced Custom Fields and Contact Form 7 to display a form

I want my users to be able to put a Contact Form 7 shortcode into a custom field in the Wordpress editor. I've created the custom field using ACF and I can pull the value onto the page, but when I try to include it in the shortcode, it comes back with a 404.
This code:
<?php echo do_shortcode(get_field('contact_form_shortcode')); ?>
Returns:
[contact-form-7 404 "Not Found"]
If I create a variable out of the value like this:
<?php
$formCode = get_field('contact_form_shortcode');
echo $formCode;
?>
The echo returns:
[contact-form-7 id="473" title="Learn More Form"]
But I get the same 404 after putting that value into the echo do_shortcode function list this:
<?php echo do_shortcode($formCode); ?>
What am I missing?
To do it With ACF pro plugin and without other extra plugins.
Create a relation field ( example: contact_form )
add the below code into your page loop:
<?php $posts = get_field('contact_form');
if( $posts ):
foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT)
$cf7_id= $p->ID;
echo do_shortcode( '[contact-form-7 id="'.$cf7_id.'" ]' );
endforeach;
endif; ?>
I was able to resolve this issue by using the technique I discussed in my comment above. By using the WYSWIG field set to 'Run filter "the_content"' I'm able to pull the field value in the way I want it. The only drawback is that users could type something else in there besides a form shortcode.
Here's my final code:
<?php
if (get_field('contact_form_shortcode')):
echo get_field('contact_form_shortcode');
else:
echo do_shortcode('[contact-form-7 id="473" title="Learn More Form"]');
endif;
?>
Instead of using a WYSIWYG field, you can simply set a Text field to "Convert HTML into tags" under the Formatting setting on the field. This will stop the 404 errors from CF7 and properly process the form. The WYSIWYG fields tend to be too hard to control from bad user input.
Here's my solution:
Create a "Post Object" type Field which will returns the post ID.
the Filter by Post Type for this field should be sett to "Contact Form" and Filter by Taxonomy leave empty.
Then in page template: (php)
<?php $contact_form = get_field('contact_form'); ?>
HTML:
<div class="col-12 col-md-8 offset-md-2">
<?php echo do_shortcode('[contact-form-7 id="'. $contact_form .'"]'); ?>
</div>
acf field screenshot
Another Solution :
1) Install https://github.com/taylormsj/acf-cf7
Installation
> 1 Copy the acf-cf7 folder into your wp-content/plugins folder
> 2 Activate the Contact Form 7 plugin via the plugins admin page
> 3 Create a new field via ACF and select the Contact Form 7 type
> 4 Please refer to the description for more info regarding the field type settings
2) Insert following code into template :
<?php if( get_field('field_acf') ):
the_field('field_acf');
endif; ?>
This applies to create a contact form 7 in a modal or Pop Up, multiple forms on the same page.
To paste CF7 shortcode (or any other generated shortcode) into ACF field as you were asking, just use the #armadadrive solution, which is exactly what question was about and it works.
create ACF text field with some name, eg hero_form
paste your shortcode into it
display contents like so <?php echo do_shortcode(get_field('hero_form')); ?>
Thanks #armadadrive
Here you can try please check the code for the shortcode below:
<?php echo do_shortcode(get_field('YourACFfieldNameHere')); ?>
I tried it on my website and it works.

Categories