Getting post id in WP_Query - wordpress

I am trying to execute the query in WordPress to retrieve the last post from featured category, then check for its custom field and show an image stored in it.
Well, my code is not working for some reason.Can you spot any error there? Or point me in the right direction?
<?php $featured = new WP_Query('showposts=1&category_name=featured'); ?>
<?php if($featured->have_posts()) : ?>
<?php while($featured->have_posts()) : $featured->the_post(); ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<img src="<?php echo get_post_meta($post->ID, 'image3', true); ?>"></a>
<?php endwhile; ?>
<?php endif; ?>
Many thanks!

I hope this will do what you expecting
$args['category_name']='featured'; // posts from particular category
$args['orderby']='date'; //to get latest post
$args['order']='DESC';
$args['numberposts']='1';// to get only one post from post list
$featured = new WP_Query($args);

Related

Plotting ACF repeater on a Google Map

Im building out a wordpress site that plots custom data on a google map using Advanced Custom Fields Pro but can't get it to generate the map using the custom page I have created.
I have created a Google Map Picker with ACF which I have assigned to a post category type ID 4. This is functioning as expected and I can pick the location for each post
In my custom template for wordpress site I have entered the below code to call any location information from the Category in question in this case its ID is 4
This Section pulls and displays the location as expected on the page ( I will be hiding it in the final build)
<?php
$catquery = new WP_Query( 'cat=4&posts_per_page=10' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<?php the_field('location'); ?>
<?php endwhile; ?>
</div>
However the map just pulls the first blog an does not display the other
Below is the code im using to call the location and pass it into the map
<?php if( have_rows('sdg_location') ): ?>
<div class="acf-map">
<?php while ( have_rows('sdg_location') ) : the_row();
$location = get_field('location');
?>
<div class="marker" data-lat="<?php echo $location['lat']; ?>"
data-lng="<?php echo $location['lng']; ?>">
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
Can anyone advise of a better method of doing this? Is there a way to pass the location from the categories directly into the map call?
Below is a screenshot of whats happening can anyone advise?
Thanks a million for your help in advance
Screenshot of map and locations loading
Ended up using this code
<? $map_posts = get_posts(array(
'post_type' => 'post',
'cat' => '4'));
if( $map_posts ): ?>
<div class="acf-map">
<?php foreach( $map_posts as $map_post ) :
$location = get_field('location', $map_post->ID);
?>
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
</div>
<?php endforeach; ?>
</div>

Displaying a custom post type title within another loop

I have a Custom Post Type set up called Venues. I'm also using a plugin called Event Organiser, and I want to display the title and link to the venue within one of the Event Organiser templates.
The code is:
<?php if( $eo_event_loop->have_posts() ): ?>
<ul <?php echo $id; ?> class="<?php echo esc_attr($classes);?>" >
<?php while( $eo_event_loop->have_posts() ): $eo_event_loop->the_post(); ?>
<?php
//Generate HTML classes for this event
$eo_event_classes = eo_get_event_classes();
//For non-all-day events, include time format
$format = ( eo_is_all_day() ? $date_format : $date_format.' '.$time_format );
?>
<li class="<?php echo esc_attr(implode(' ',$eo_event_classes)); ?>" >
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" ><?php the_title(); ?></a> at VENUE NAME HERE <?php echo __('on','eventorganiser') . ' '.eo_get_the_start($format); ?>
</li>
<?php endwhile; ?>
</ul>
Where VENUE NAME HERE is, I want the title and link to my custom post type. Might be just something really simple I'm missing, but any help much appreciated.
I used Advanced Custom Fields plugin and used 'Relationship' field so that I could choose a Venue for each event. Then used this code to bring through the Venue link:
<?php $venue = get_field('location_venue'); ?>
<?php foreach( $venue as $venue ): ?>
<?php echo get_the_title( $venue->ID ); ?>
<?php endforeach; ?>
More info available here:
http://www.advancedcustomfields.com/resources/tutorials/querying-relationship-fields/

display posts under specific page

I am having many number of pages, I would like to display some specific posts under one specific page.
for example Under News page i need to display only news related posts only
how to do this?
This is a new query, won't effect the main Wordpress loop and can be used multiple times on a page. Change parameters to your category name and number of posts to show. Use in a page template or in page editor with php execution enabled.
<?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
<?php the_content(); ?>
<?php endwhile; ?>

wordpress featured posts

i'm trying to create a portfolio website using wordpress,
each post has view costum fields, one of which is called type - with the value of "featured" or "not-featured"
now when user clicks on the post title - they go the the single.php to see the entire post, here i would love to display all featured thumbnails
i tried this
<?php while ( have_posts() ) : the_post() ?>
<?php if(get_post_meta($post->ID, 'type', true) == "featured") {; ?>
<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'your-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark">
<img src="<?php echo get_post_meta($post->ID, 'intro_thump', true); ?>" alt="Icon for Post #<?php the_ID(); ?>" />
</a></h2>
<?php }; ?>
<div class="entry-content">
</div><!– .entry-content –>
<?php endwhile; ?>
(THIS CODE IS SIMILAR TO THE CODE I USE AT INDEX.PHP AND THERE IT DOES WORK, HERE AT SINGLE.PHP IT DOES NOT WORK)
but this does not display all the thumbnails (only the current posts' thumbnail (is it's a feature post))
this is my first attempt of trying to create a theme from blank so i'm not sure what the error could be
thanks for your help
The code in your question only loops through the posts returned by the query made for the current view, in the case of a single post view that is one post. You want to perform a new query to retrieve all the posts that have the required meta value:
<?php
query_posts(array("meta_key" => "type", "meta_value" => "featured"));
if (have_posts()) : while (have_posts()) : the_post();
?>
<!-- Display thumbnails -->
<?php endwhile; endif; ?>

Wordpress - Force most recent posts first

I want to use the plugin WP Smart Sort to sort my posts alphabetically, except I need the most recent posted first on the home page. The relevant existing code for the home page is below. Is there something I can add to it to force it to include the recent posts and go by date rather than by the alphabetical order I want everywhere else?
(In case it helps to see the context, the site is at http://mormonscholarstestify.org/)
<div id="homebox">
<?php if ($aOptions['homebox-id'] != '') { query_posts('showposts=5&cat=' . $aOptions['homebox-id']); } ?>
<?php if(have_posts()) : the_post() ?>
<div id="boxmain">
<img src="<?php $key="thumbnail"; echo get_post_meta($post->ID, $key, true); ?>" alt="<?php the_title() ?>" />
<h3><?php the_title() ?></h3>
<?php $key="affiliation"; echo get_post_meta($post->ID, $key, true); ?><p>
<?php the_excerpt(); ?>
</div>
<?php endif; ?>
<?php while(have_posts()) : the_post() ?>
<div class="boxitem">
<img src="<?php $key="thumbnail"; echo get_post_meta($post->ID, $key, true); ?>" alt="<?php the_title() ?>" />
<h3><?php the_title() ?></h3>
<?php $key="affiliation"; echo get_post_meta($post->ID, $key, true); ?>
</div>
<?php endwhile; ?>
</div>
According to the Wordpress query_posts documentation you should be able to supply an orderby parameter to the querystring to change how the posts are ordered, like so:
query_posts('showposts=5&orderby=date&order=ASC&cat=' . $aOptions['homebox-id']);

Resources