I'm struggling with Wordpress and Advanced Custom Fields - php

I've got some PHP code which I'm using in a WordPress website. Here's the code:
<h3>Case Studies</h3>
<?php
$the_query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => -1,
'cat' => 3,
'meta_key' => 'sector',
'orderby' => 'meta_value',
'order' => 'ASC'
));
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo get_permalink(); ?>">
<h1><?php the_field('client_name'); ?></h1><p><?php the_field('sector'); ?></p>
<span style="background-image:url(<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo $url; ?>)"></span>
</a>
<?php endwhile; endif; ?>
<h3>Other Clients</h3>
<?php if( have_rows('clients') ):
while ( have_rows('clients') ) : the_row(); ?>
<a>
<h1><?php the_sub_field('client'); ?></h1><p><?php the_sub_field('sector'); ?></p>
<span></span>
</a>
<?php endwhile; endif; ?>
So - at the top, we've got "Case Studies" and this is just pulling in some details from the posts on the website (the client name and the sector).
Next, I've got "Other Clients" - this is set up as an Advanced Custom Field on the page that this code appears on. Pretty simple as well.
Now, here's the fun:
It works if I reverse the two sections ("Other Clients" first) but not this way - any ideas what's going wrong? I'm presuming it's something in the "Case Studies" section that's messing up the following one, but I'm at a loss. If I can provide any more info, let me know!
Much thanks in advance x

After finishing your custom query you must call wp_reset_postdata();
$the_query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => -1,
'cat' => 3,
'meta_key' => 'sector',
'orderby' => 'meta_value',
'order' => 'ASC'
));
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo get_permalink(); ?>">
<h1><?php the_field('client_name'); ?></h1><p><?php the_field('sector'); ?></p>
<span style="background-image:url(<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo $url; ?>)"></span>
</a>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
Because your custom query $the_query->the_post(); overrides global $post object, after finishing your query must always do this.

Related

WordPress add a class for newest posts

Does sombody know how i can add a class to the newest posts in WordPress. And what i mean with the newest is the lasts posts that are added in the last minut?
My code now:
<?php
// query
$the_query = new WP_Query(array(
'post_type' => 'post',
'category_name' => 'profiel',
'posts_per_page' => 48,
'paged' => $paged,
'meta_key' => 'online',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
if ( $the_query->have_posts() ) : ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div id="post-<?php echo $post->ID; ?>" class="profiel">
<h3><?php the_title(); ?></h3>
</div>
<?php endwhile; ?>
<?php else : ?>
<div class="404 not-found">
<h3>Not Found</h3>
<div class="post-excerpt">
<p>Sorry, but there are no more posts here... Please try going back to the main page</p>
</div>
</div>
<?php endif;
wp_reset_query();
the_posts_pagination( array(
'mid_size' => 10
) );
?>
Get the current time, remove 60 seconds compare that to the timestamp of your post. If your post timestamp is greater than the timestamp from a minute ago then it's new.
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div id="post-<?php echo $post->ID; ?>" class="profiel <?= (get_the_time('U') > (time()-60)) ? 'new' : '' ?>">
<h3><?php the_title(); ?></h3>
</div>

Show post if true/false is yes. Advanced custom fields

I have managed to get my loop to show only the posts that display true on an advanced custom field.
But I now only want to show one post. I cant seem to get it to only loop one of the posts that features the true/false field as yes.
'posts_per_page' => '1'
Doesn't work as it only shows the latest post.. which if its not ticked it just shows blank.
<?php
$args = array(
'post_type' => 'event'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if ( 'yes' == get_field('sponsored_event') ): ?>
<div class="sponsored-event">
<div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);">
</div>
<div class="sponsored-info">
<h2>Sponsored Event</h2>
<h1><strong><?php the_title(); ?></strong></h1>
<p><strong>Date</strong></p><br>
<p class="place"><?php the_field( 'event_location' ); ?></p>
<p class="time"><?php the_field( 'event_time' ); ?></p>
<p><?php the_field( 'excerpt' ); ?></p>
</div>
</div>
<?php endif; ?>
<?php endwhile; else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
You should use Meta Query here. Change your args array to:
// args
$args = array(
'numberposts' => 1,
'post_type' => 'event',
'posts_per_page' => '1'
'meta_key' => 'sponsored_event',
'meta_value' => 'yes'
);
The ACF Radio button Yes/No field is to be manipulated the other way. You have to get the Output and then compare with the value that you need.
Syntax:
$variable = get_field('field_name', $post->ID);
Where the $post->ID will be appering from the Loop that you use.
if (get_field('sponsored_event') == 'yes') {
// code to run if the above is true
}
else
{
// code for else part
}
Make sure that the value saved into the DB for the radio button is like you give in the if statement
This is an Optional for you to use the meta_value in the Query. if you dod't use you can fetch the acf value with the help of the post ID that you get from the loop of Wp_Query
Change the Wp_Query like this if you need only one post that to the latest one that has been stored.
$args = array( 'post_type' => 'event', 'posts_per_page' => 1,'order'=>'DESC','orderby'=>'ID','meta_key'=> 'sponsored_event','meta_value'=>'yes');
Else if you want all the posts that has been stored you can use like this.
$args = array( 'post_type' => 'event', 'posts_per_page' => -1,'order'=>'DESC','orderby'=>'ID','meta_key'=> 'sponsored_event','meta_value'=>'yes');
Note:
post_per_page=10 -> Will bring the 10 posts from your post type
post_per_page=-1 -> will bring infinite posts that your post type has
Entire Loop:
<?php
$args = array(
'posts_per_page' => 1,
'post_type' => 'event',
'meta_key' => 'sponsored_event',
'meta_value' => 'yes'
);
$the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="sponsored-event">
<div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);">
</div>
<div class="sponsored-info">
<h2>Sponsored Event</h2>
<h1><strong><?php the_title(); ?></strong></h1>
<p><strong>Date</strong></p><br>
<p class="place"><?php the_field( 'event_location' ); ?></p>
<p class="time"><?php the_field( 'event_time' ); ?></p>
<p><?php the_field( 'excerpt' ); ?></p>
</div>
</div>
<?php endwhile; else: ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
Your If statement in the query seems to be Incorrect and i have added the Query Executed output over to that if statement.

Sort posts by name in Wordpress

For some reason, no matter what I do, nothing seems to work to put these posts by alphabetical order. The code is:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
array( 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC' );
?>
<li>
<div class="sponsor-thumb">
<a href="'.get_permalink().'">
<?php the_post_thumbnail( 'category-thumb' ); ?></a></div>
<a href="<?php the_permalink() ?>">
<?php the_title(); ?>
</a></li>
<?php endwhile; else: ?>
<p><?php _e('Sorry, this page does not exist.'); ?></p>
<?php endif; ?>
Putting that code where you have it is having no effect on the query.
To achieve what you want you want to use WP_Query like so:
$args = array( 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC' );
$my_query = new WP_Query( $args );
if ( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post();
// do stuff here
endwhile;
endif;

Wordpress: archive multiple post types

I'm trying to figure out if it's possible to archive multiple post types on a page, I have an individual archive for each of the post types working fine, but I also want another page that will archive both of them. I'm still quite new to WP so I'm not at all sure if it's possible but what I'm doing so far isn't working correctly:
<?php query_posts('post_type=type01'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink(); ?>">
<div class="type01-div" data-value="<?php
$date = DateTime::createFromFormat('dnY', get_field('type01_date_select'));
echo $date->format('dnY');
?>">STUFF HERE</div>
</a>
<?php endwhile; endif; ?>
<?php query_posts('post_type=type02'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink(); ?>">
<div class="type02-div" data-value="<?php
$date = DateTime::createFromFormat('dnY', get_field('type02_date_select'));
echo $date->format('dnY');
?>">STUFF HERE</div>
</a>
<?php endwhile; endif; ?>
So all the posts from 'type01' are showing up, but the posts from 'type02' aren't. Is it possible to archive both? In separate loops though as each post type will be wrapped in a different div class.
You need to reset your query for the next loop, add this between your loops:
<?php wp_reset_query(); ?>
I have a similar page like this and used this code to do it:
<h2>type01</h2>
<?php
$args = array(
'post_type' => array( 'type01' ),
'order' => 'asc',
'orderby' => 'title',
'posts_per_page' => -1
);
$loop = new WP_Query( $args );?>
<?php while ( $loop->have_posts() ) : $loop->the_post();?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
<h2>type02</h2>
<ul>
<?php
$args = array(
'post_type' => array( 'type02' ),
'order' => 'asc',
'orderby' => 'title',
'posts_per_page' => -1
);
$loop = new WP_Query( $args );?>
<?php while ( $loop->have_posts() ) : $loop->the_post();?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
Check out this link for more info: http://codex.wordpress.org/Function_Reference/wp_reset_query

Can't filter on category in loop when using Custom Post Type in Wordpress

I'm trying to display posts from a custom post type called portfolio using the code below and then filter the result by category, I have tried putting - category_name , catid, portfolio_category in the array
and have had a look around the forums and tried a few things but cant seem to get it to work , it either displays nothing or all the post from all categories.
<?php
$args=array(
'post_type' => 'portfolio',
'post_status' => 'publish',
'posts_per_page' => 7,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to
<?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
The registered taxonomy is portfolio_category, any help on this would be appreciated , many thanks
You could try to call the the category as a custom taxonomy:
'portfolio_cat' => 'name_of_your_category'
After trying a few things this is how i got it to work using the 'tax_query' hope this helps someone...
<?php
$args = array(
'post_type'=>'portfolio',
'tax_query' => array(
array(
'taxonomy' => 'portfolio_category',
'field' => 'slug',
'terms' => 'solutions'
)
)
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to
<?php the_title_attribute(); ?>">
<?php the_title(); ?></a></p>
<?php the_excerpt()?>
<?php echo get_the_post_thumbnail($post->ID, array(50,50)); ?>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>

Categories