Ask your WordPress questions! Pay money and get answers fast! (more info)

Get post type's custom fields WordPress

  • SOLVED

Hello, I am using a custom post type (home_page) and the Advanced Custom Fields plugin (http://www.advancedcustomfields.com).

I would like to show the custom fields from the oldest post.

The reason I want to show info from the oldest post is that I will expire and delete posts on a certain date. I will display these custom fields on my home page.

I have got the below code to work on my home page, getting the fields from the post ID, but how can I choose the oldest post?


<style type="text/css"> body {background-image: url(<?php echo do_shortcode('[acf field="background_image" post_id="7399"]'); ?>);}</style>

<div class="homemain">
<div class="homemainbox">
<div class="homemaintitle"><?php echo do_shortcode('[acf field="title" post_id="7399"]'); ?></div>
<div class="homemainsubtitle"><?php echo do_shortcode('[acf field="description" post_id="7399"]'); ?></div>
<div class="homemainurl"><a href="<?php echo do_shortcode('[acf field="link" post_id="7399"]'); ?>">Click here to find out more</a></div>

Answers (4)

2014-06-03

Hariprasad Vijayan answers:

Hello,

It seems like the post id is hardcoded in the page itself. You can see the post id is passing to the short code like 'post_id="7399"'.

You can use the following code for getting oldest post from custom post type home_page and run shortcode based on that post's id.

<?php
$args = array(
'post_type' => 'home_page',
'posts_per_page' => 1,
'order' => 'ASC'
);
// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
<style type="text/css"> body {background-image: url(<?php echo do_shortcode('[acf field="background_image" post_id="'.get_the_ID().'"]'); ?>);}</style>
<div class="homemain">
<div class="homemainbox">
<div class="homemaintitle"><?php echo do_shortcode('[acf field="title" post_id="'.get_the_ID().'"]'); ?></div>
<div class="homemainsubtitle"><?php echo do_shortcode('[acf field="description" post_id="'.get_the_ID().'"]'); ?></div>
<div class="homemainurl"><a href="<?php echo do_shortcode('[acf field="link" post_id="'.get_the_ID().'"]'); ?>">Click here to find out more</a></div>
<?php
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>

2014-06-03

Romel Apuya answers:

hi,

$args = array('orderby'=>'date','order'=>'ASC','posts_per_page'=>1,'caller_get_posts'=>1,'post_type'=>'home_page');
$oldestpost = get_posts($args);

<style type="text/css"> body {background-image: url(<?php echo do_shortcode('[acf field="background_image" post_id="{$oldestpost->ID}"]'); ?>);}</style>
<div class="homemain">
<div class="homemainbox">
<div class="homemaintitle"><?php echo do_shortcode('[acf field="title" post_id="{$oldestpost->ID}"]'); ?></div>
<div class="homemainsubtitle"><?php echo do_shortcode('[acf field="description" post_id="{$oldestpost->ID}"]'); ?></div>
<div class="homemainurl"><a href="<?php echo do_shortcode('[acf field="link" post_id="{$oldestpost->ID}"]'); ?>">Click here to find out more</a></div>

2014-06-03

Dbranes answers:

You can try the following:


$oldest = get_posts( array( 'posts_per_page' => 1, 'post_type' => 'home_page', 'orderby' => 'date', 'order' => 'ASC' ) );

if( isset( $oldest[0] ) ):
$oldest_pid = $oldest[0]->ID;
else:
$oldest_pid = 0;
endif;


and then you can use <em> $oldest_pid</em> in your ACF shortcode logic.

<strong>Update:</strong>

Or if you prefer a simple SQL query:

global $wpdb;
$oldest_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_type = 'home_page' ORDER BY post_date ASC LIMIT 1" );

2014-06-03

timDesain Nanang answers:

assume: you will show the posts (home_page) between :

<strong>January 1st, 2013 - March 1st, 2013</strong>

change <strong>'posts_per_page' => -1,</strong> with your purpose


$args = array(
'post_type' => 'home_page',
'post_status' => array('publish'),
'posts_per_page' => -1,
'ignore_sticky_posts' => 1,
'orderby' =>'date',
'order' =>'ASC',
'date_query' => array(
array(
'after' => array(
'year' => 2013,
'month' => 1,
'day' => 1,
),
'before' => array(
'year' => 2013,
'month' => 3,
'day' => 1,
),
'inclusive' => true,
),
),
);
$wpq = new WP_Query( $args );
if ( $wpq->have_posts() ) {
while ( $wpq->have_posts() ) {
$wpq->the_post();
$post_id = get_the_ID();
?>
<div class="homemainbox">
<div class="homemaintitle"><?php echo do_shortcode('[acf field="title" post_id="'.$post_id.'"]'); ?></div>
<div class="homemainsubtitle"><?php echo do_shortcode('[acf field="description" post_id="'.$post_id.'"]'); ?></div>
<div class="homemainurl"><a href="<?php echo do_shortcode('[acf field="link" post_id="'.$post_id.'"]'); ?>">Click here to find out more</a></div>
</div>
<?php
}
}
wp_reset_postdata();