Wordpress query_posts displayed by category custom field - php

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; ?>

Related

Wordpress ACF Repeater sub fields

I am having an issue in Wordpress with ACF Repeater sub-fields link object not returning href on front-end. Basically the link in the front-end does not output any links set in WP admin. Text output is working.
Repeater fields:
Field label = Link to doc
Field name = link_to_doc
Field type = Repeater
Repeater sub-fields 1:
Field label = Link
Field name = link
Field type = Link
Repeater sub-fields 2:
Field label = Text of link
Field name = text_of_link
Field type = Text
PHP code:
<?php if ($section['link_to_doc']) : ?>
<?php foreach ($section['link_to_doc'] as $link) : ?>
<div>
<a href="<?= $link['link'] ?>" class="btn-txt">
<?= $link['text_of_link'] ?>
</a>
</div>
<?php endforeach; ?>
<?php endif; ?>
Could anyone check the php code and let me know what's wrong?
This topic has no answer for a while, you've probably found a solution. But now ACF is explaining how to access the data for nested repeaters directly in the documentation.
<?php
// Loop over the first repeater
if( have_rows('type') ):
while( have_rows('type') ) : the_row();
// Loop over sub repeater rows.
if( have_rows('type') ):
while( have_rows('type') ) : the_row();
// Get sub value.
$child_title = get_sub_field('name');
endwhile;
endif;
endwhile;
endif;
There is also other ways to access this kind of data, here's the list:
https://www.advancedcustomfields.com/resources/repeater/

Randomize array content in PHP via Wordpress posts

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
}
?>

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();
?>

Sort the category with custom field

I want to sort the category using custom field value. So when I type newcat as name, I can define the category from the value box.
The loop is like this
<?php query_posts ('ignore_sticky_posts=1&showposts=10&cat=XXXXXX');
if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
And the custom field call by this code:
<?php $newcat = get_post_meta($post->ID, 'newcat', true); ?>
<?php echo $newcat; ?>
How can I change the XXXX on the loop with the custom field code? Please help. I'm not smart on php. Thanks in advance
I'm not very sure that I understand what you want to do.
Assuming that your custom field meta key is 'newcat', you may like to try to replace:
<?php query_posts ('ignore_sticky_posts=1&showposts=10&cat=XXXXXX');
with:
<?php query_posts ('meta_key=newcat&orderby=meta_value&order=ASC&ignore_sticky_posts=1&showposts=10');

Categories