Randomize array content in PHP via Wordpress posts - php

Do you guys know how to limit posts per page and randomize posts in wordpress?
I have a relationship field in the back-end where I add and remove items that I created to display in a website. This content is printed through the WP_Query below:
<?php
$args = array (
'post_type' => 'home_banners'
$fullbanner = new WP_Query ( $args );
?>
And here is the PHP:
<?php if ( have_posts() ) : while ( $fullbanner->have_posts() ) : $fullbanner->the_post(); ?>
#####Get the relationship field
<?php $banners = get_field('home_banner_01_selection'); ?>
#####Check if the relationship field has contents
<?php if( $banners ): ?>
#####Start foreach
<?php foreach( $banners as $banner ): ?>
<?php the_field( 'home_headline', $banner->ID ); ?>
<?php endforeach; ?>
<?php endif; ?>
I have three contents added in this relationship field. I want just to display ONLY ONE content per page and randomize it when refreshing the page. At the moment it is currently display all the three contents in the page.
I notice that the var $banners behave as an array. If I add echo count($banners); it will display 3. Moreover, if I add shuffle($banners); it will shuffle the content among them.
Thanks for helping me.

You could shuffle the array $banners and then just echo out the post in array wit
<?php
$banners = get_field('home_banner_01_selection');
if($banners) {
shuffle($banners);
?>
<?php the_field( 'home_headline', $banners[0]->ID ); ?>
<?php
}
?>

Related

Store the output of foreach loop to array

I'm using a code to show specific filtered products and i'm trying to integrate this code with an exist function in my theme showing posts in slider . what i'm stuck in is that's i need the output of the foreach loop to be stored in array so i can use it in the function , sorry for my english , here's my code , any help will be highly appreciated
//the loop and it output posts with no problems//
<?php
foreach ( $products as $post ) : setup_postdata( $post ); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endforeach; // end of the loop. ?>
<?php wp_reset_postdata(); ?>
//the slider function and it works through the theme in same logic for example for related products the value is an array //
<?php
$slider_args = array(
'slides_per_view' => 4 ,
'img_size' => 'woocommerce_thumbnail',
'custom_sizes' => false
);
theme_set_loop_prop( 'products_view', 'carousel' );
echo theme_generate_posts_slider( $slider_args, false, //i need to put the array here// $relatedproducts );
?>

Wordpress query_posts displayed by category custom field

Have wordpress based site, where:
MainCategory
Subcategory(custom-field value = custom1)
Subcategory2(custom-field value = custom2)
Subcategory3(custom-field value = custom3)
MainCategory2
Subcategory(custom-field value = custom1)
Subcategory2(custom-field value = custom2)
Subcategory3(custom-field value = custom3)
MainCategory3
Subcategory(custom-field value = custom1)
Subcategory2(custom-field value = custom2)
Subcategory3(custom-field value = custom3)
As you can see, all subacegories in main categories are the same. With same name (not slug), and there are custom fields with same field values.
I need to display posts that is in MainCategory2 AND subcategory has custom field with value custom2. Is this possible?
P.S. I use ACF plugin for custom fields.
I'm not sure if this is the best solution, but it will hopefully solve your problem.
Loop all posts of MainCategory2 (let's assume, this category has the ID 2)
Check if the content of the custom-field is equal custom2.
Define what should be looped (in this example it's the blogtitle and link)
The code would look like this:
<?php query_posts( 'showposts=20&cat=2&order=ASC' ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if( get_field('custom-field') == 'custom2' ): ?>
<?php the_title(); ?><br />
<?php else : ?>
<?php endif; ?>
<?php endwhile; endif; ?>

ACF: Display Fields in a template based on Cpt ui

I have created a custom post type based on CPT Ui I am tryin to display the items of that in a template ideally what I want to be able to do is
www.siteurl.com/profile/profilename
Hence why I have created a template page with the following code.
But I dont no how I would get profilename into my query posts and then display the relivent data this is what I have tired so far to get id and permalink to display but to no avail.
I really want to be able to do it like how the documentation states. Ideally i would suspect my best approach would be to check if user has created a profile in the front end if not direct them to do so?.
Display a field
<p><?php the_field('field_name'); ?></p>
http://www.advancedcustomfields.com/resources/code-examples/
<?php
/**
* Template Name: Profile Page
*/
get_header(); ?>
<?php get_template_part( 'page', 'title' ); ?>
<div class="width-container">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'profiles',
'meta_key' => 'profilename',
'meta_value' => 'david'
));
if($posts)
{
echo '<ul>';
foreach($posts as $post)
{
echo '<li>' . get_the_title($post->ID) . '</li>';
}
echo '</ul>';
}
?>
<?php get_sidebar(); ?>
</div><!-- close .width-container -->
<?php get_footer(); ?>
If anyone has done anything simlar maybe I should be using current user but again i dont no how to link that up to acf as I am new to that plugin
Perhaps you could try another approach and build a custom query with WP_Query instead. The example bellow use the WP_Query object rather than the get_posts function, however the arguments and logic remain the same.
This implies we will find all posts that have a post_type of profiles where the ’custom field’ profilename is equal to david. The custom field ‘profilename’ in this case could be a text field, a radio button or a select field (something that saves a single text value).
<?php
/**
* Template Name: Profile Page
*/
get_header();
$user = get_post_meta($post->ID, 'profilename', true); // saving the field value in a $variable can help to build a more dynamic WP_Query...
?>
<?php get_template_part( 'page', 'title' ); ?>
<div class="width-container">
<h1>__('Listing Posts by:', 'text_domain');</h1>
<?php echo '<p class="text-center">' . esc_html( $user ) . '</p>'?>;
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'profiles',
'meta_key' => 'profilename',
'meta_value' => $user // retrieve the actual profilename field value
);
// query
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
<?php get_sidebar(); ?>
</div><!-- close .width-container -->
<?php get_footer(); ?>
The code above is an example of how to query ("get and display") posts in WP using WP_Query object within a template based on custom field called ‘profilename’ which has a value of ‘david’ or whatever data is saved inside the field as per your example.
Perhaps you want to display the relevant data (values) of the custom fields you've created for the WP default user profile page, and if this is the case, you should follow another approach and instead of query posts you simply output the fields a user might fill on their profile. In order to that you could Get values from a user.
This example will display the field 'profilename' value from a user with an ID of 1.
<?php the_field('field_name', 'user_1'); ?>
Within your loop you could use $user_ID = get_current_user_id(); to retrieve the user_id as follows:
<?php
/**
* Template Name: Profile Page
*/
get_header();
$user = get_post_meta($post->ID, 'profilename', true); // saving the field value in a $variable can help to build a more dynamic WP_Query...
$user_ID = get_current_user_id();
?>
<?php get_template_part( 'page', 'title' ); ?>
<div class="width-container">
<h5>__('Listing Posts by:', 'text_domain');</h5>
<h1><?php the_field('profilename', '$user_ID'); ?>;</h1> // this will retrieve the data/value saved in the 'profilename' field for the current user
...
Check ACF's Query Posts by Custom Fields documentation for other ways to retrieve an array of post objects from the database using native WP functions.

PHP/WordPress: Custom Field Value in Loop returns Array instead of Single Result

For the search results page in WordPress Im making a custom template. And in that custom template I want to display custom meta field values.
However, when I do this:
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
$pName = get_post_meta($post->ID, $productName, true);
echo $pName;
);
?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif;
The result I get is not the value of the custom field but:
Array
When I var_dump the $pName it shows all the right custom field content.
Question: Why am I getting an array as the result (when Ive told it its a single result with 'true') and how do I fix it so it displays the proper content?
Thanks!

Wordpress - Displaying content from custom posts

Im using advanced custom fields and have setup a custom post type for testimonials, on the testimonial page there is a relationship field (display_on_page) where you would select which page to display the testimonial on.
The issue is that the testimonial displays on everypage when a page is chosen
Does anyone know what I am doing wrong on the below please?
<?php query_posts( 'post_type=Testimonial'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $posts = get_field('display_on_page');
if( $posts ): ?>
<?php the_field('the_testimonial'); ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
If I understood correctly, you should check if the value that you get from get_field('display_on_page') matches the current page ID.
(I'm assuming that the page ID is what is stored by the custom field you created).
If that's the case, you may query your posts filtering by custom field values, like such:
<?php
// Fetches current page ID, assuming that this is page.php or similar
$current_page_id = get_the_ID();
query_posts(
'post_type=Testimonial',
'meta_key' => 'display_on_page',
'meta_value' => $current_page_id // Only gets Testimonials that are set for this page
);
while ( have_posts() ) : the_post();
the_field('the_testimonial');
endwhile;
// Resets default WP Post Data *AFTER* the loop is complete
wp_reset_postdata();
?>

Categories