Wordpress custom taxonomy logic - wordpress

I'm sitting here with an issue to figure out. I'll explain it shortly:
On the homepage of a webiste i am developing , i have a few tiles with brands which link to a custom taxonomy ( with some products arranged by current brand, some info , and a slider image). The slider image is the issue. Becouse this is a custom taxonomy it generates each brands page. So when it is trying to the_field('header-image') inside the loop, it returns with all images , even if i add a posts_per_page = 1 to the wp_query $args. Then it will display the first image to all brands.
<div class="image-container">
<?php
$args = array(
'post_type' => 'brands',
'posts_per_page' => 1,
'order' => 'ASC'
);
$myPosts = new WP_query($args);
if ($myPosts->have_posts()) : while ($myPosts->have_posts()) : $myPosts->the_post();
?>
<img src="<?php the_field("header-image"); ?>" alt="">
<?php
endwhile; endif;
wp_reset_query();
?>
</div>
Does anyone have a clue ?
Thanks in advance,
Dimotro

I think you need to get deep understanding about post_type, taxonomy and terms or may you can check the acf documentation about displaying that fields here

Related

Filtering Products of same price in one page wordpress

So i have an online store with variations of products but ill like to have a page that shows products with a price range. Say I have a page called 3k Store. I want such that when a user goes to that page, it filters and displays only goods that cost 3000, Please how do i go about this? I am using wordpress and woocommerce. Thanks.
1. Create a page template to list products
Create a page template to be able to put some code on it. Name it page.list-product-in-range.php for example and add this code at the start of the file to make it a template :
<?php // Template Name: List product in range ?>
<?php get_header(); ?>
<?php get_footer(); ?>
2. Create a page in the wordpress admin
Create a page named Product that cost 3000for example and select the template previously created. That's all for the moment.
3. Go to the php file that we created in part 1
Between <?php get_header(); ?> and <?php get_footer(); ?>, add this part to get the products that cost 3000 :
( I let the range in the meta_query so you can easily set something else than 3000 by playing with the number inside value)
<?php
$products = get_posts(
array(
'posts_per_page' => -1,
'post_type' => array('product', 'product_variation'),
'meta_query' => array(
array(
'key' => '_price',
'value' => array(
3000 ,
3000
),
'compare' => 'BETWEEN',
'type'=> 'NUMERIC'
)
)
)
);
?>
4. Add the loop to list products
After the code that we added in part 3 put this code :
<ul class="products">
<?php
if (!empty($products)) {
foreach ( $products as $post ) : setup_postdata($post);
wc_get_template_part( 'content', 'product' );
endforeach;
} else {
echo __( 'No products found', 'woocommerce' );
}
wp_reset_postdata();
?>
</ul>
You also can use pre_get_posts hook to set the products in the main query or instead of get_posts() use the WP_Query class. I only use get_posts() for the simplicity .
If you need pagination (and i think you will need), you should think about using pre_get_posts or WP_Query.
Code tested and it works.

Wordpress Display Child page data: Title, featured image and custom field

I'm displaying the child pages of the current page. With the featured image, title pulling through. I also want to pull through a custom field I've added. I'm outputting this at html so it looks like each child page has its own box, with the featured image filling the box, the title on top of the image and the the custom field value sitting under the title.
But I can't get the custom field to display properly, I just get the value 1, not sure if I've converted the array properly?
Could you help me out to output the custom field 'PageTag'
https://i.stack.imgur.com/vzCBU.png
https://i.stack.imgur.com/BwfuV.png
Wordpress template code
<div class="childPages">
<?php
$subs = new WP_Query(
array(
'post_parent' => $post->ID,
'post_type' => 'page',
'meta_key' => '_thumbnail_id'
)
);
if( $subs->have_posts() ) :
while( $subs->have_posts() ) :
$subs->the_post();
$pageTag = get_post_meta($post->ID, '_PageTag' , true);
echo '<div class="childPageBlock"> '.get_the_post_thumbnail().'<div class="childPageBlockText"><h3>'.get_the_title().'</h3><span>'.print_r( $pageTag ).'</span></div></div>';
endwhile;
endif;
wp_reset_postdata(); ?>
</div>
Removing the print_r() and leaving its as
'.$pageTag.'
Worked!

wordpress get_children doesn't reflect edits to image galleries

I have a very simple theme that retrieves all image gallery links for each post using the get_children() function.
Unfortunately, there's some strange bugs - first, if images are not uploaded when creating the post and are instead selected from the media library, they don't show up after publishing. Also, if I do any edit after a post has been created, be it reordering the images, adding new ones, deleting images, even deleting the whole gallery and creating a new one, they don't show up either. Refreshing browser cache doesn't do a thing.
If i change to one of the base themes, the images show up, and the right links are there when querying the db directly.
I reproduced the basic problematic code in a one file micro-theme, but the issue still happens:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php echo $post->id;
$args = array(
'numberposts' => -1,
'order_by' => 'menu_order',
'order' => 'ASC',
'post_mime_type' => 'image',
'post_parent' => $post->ID,
'post_status' => inherit,
'post_type' => 'attachment'
);
$images = get_children($args);
if ( empty($images)){
echo "nothing";
}
foreach ( $images as $id => $image ) :
echo $image->post_title;
endforeach;
?>
<?php endwhile; ?>
<?php endif; ?>
I'm stumped, I've tried using different functions to retrieve the posts with the same result, deactivated all plugins, nothing.
I'm testing both on OSX mavericks and an Ubuntu vps, could it be some database cache thing i'm supposed to flush somewhere?
When you set 'post_parent' => $post->ID, you will only get attachments to that page. That's part of the reason not all of the images show up.
But the bigger problem is that galleries are handled differently than posts/pages/attachments. Galleries only exist in the shortcode- they're not stored in the db- so you can't query them with get_posts or get_children.
To customize the way galleries are rendered you'll have to either hook into the wordpress code that creates the gallery, or deregister that function and write your own.
This question answers how to manipulate shortcode if you were writing a plug-in- but the process will be the same from a theme.
:-)

Wordpress Custom Post Types and fields

I have two custom post types…
BOOK and BOOK AUTHOR.
I want to…
LIST all BOOK's by an author on their respective AUTHOR PAGE.
So if i have 10 books by Stephen KING. I want to list them all (and only those by him) on the STEPHEN KING page. I am having real trouble working out how to query posts to do this.
Any advice? I am using advanced custom fields plugin if that helps, but can't work out how to query and display this post information.
I currently use the following code to display ALL of my releases, but how do i get the specific ones on their specific author pages?
<?php
$args=array(
'post_type' => 'book',
'post_status' => 'publish',
'posts_per_page' => 12,
'caller_get_posts'=> 1,
'orderby'=> 'date',
'order' => 'DESC'
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '';
$i = 0;
while ($my_query->have_posts()) : $my_query->the_post();
if($i % 6 == 0) { ?>
<div class="row">
<?php
}
?>
<img src="<?php the_field('cover_image'); ?>" /><br />
<?php the_field('author_name'); ?><br />
<?php the_title(); ?><br />
</div>
<?php
if($i % 6 == 0) { ?>
<?php
}
$i++;
endwhile;
}
wp_reset_query();
?>
I did something similar, but...
main problem here is, how are you categorizing those books? Did you create taxonomies for that? And those taxonomies are in fact author´s names? I mean, how you sincronize to tell a book belong to certain author?
That is the trick. And then you can just query by category. In this case category must be same slug than author page slug. You will use that data to query post.
What i did in my case is create a function that once an author page is created, a taxonomt with same name and slug is created. So you dont need to manualy create those two things with same name and slug. Something like:
<?php$myposts = get_posts(array(    'showposts' => -1,     'post_type' => 'books',     'tax_query' => array(        array(        'taxonomy' => 'author_book',         'field' => 'slug',         'terms' => array(AUTHOR_SLUG));    )));
notice AUTHOR_SLUG could be an array, like "pete","paul".
"author_book" is just the taxonomy slug. In your case must be just "category_cat"
You can use those args with yout loop.

Pulling back a list of posts from an array in add_meta_box

So I've got a Custom Post Type (called online-cpt) with an add_meta_box of "_useraccess"
This meta box is currently happily holding comma-separated values entered and updated by the admin e.g. 2,3,14,56,6 etc.
How can I now pull back a loop of all online-cpt posts that have say the value "3" somewhere in the _useraccess metabox?
I believe its something like this, but I just can't get the 'value' meta_query to work. Anyone spot anything wrong with the following?
<?php
// The Query
$args = array('post_type' => array('online-cpt'),
'meta_query' => array(array(
'key' => '_useraccess', 'value'=> '3', 'compare' => 'IN')
));
$show_onlinecpt = new WP_Query ($args);
// The Loop
if($show_onlinecpt->have_posts()) :
while ( $show_onlinecpt->have_posts() ) :
$show_onlinecpt->the_post(); ?>
<a href="<?php the_permalink() ?>">
<h6><?php the_title(); ?></h6>
<?php
endwhile; endif;
wp_reset_postdata(); ?>
Or am I missing something else?
I've already had a look here: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
Thanks
Right, I've actually solved it myself by using get_posts instead - it pulls back all of the custom posts and then when it loops the foreach, I just reference the meta value itself.
Not sure why wp_query wouldn't work as above, but kudos to me!

Resources