Variable returns boolean false w/var_dump() - php

I am trying to feed in a tax_query argument with a variable pulling from a Custom Field. Yet nothing is returned. I did a var_dump on my variable and it keeps returning "boolean:False". So I thought maybe I needed to run a loop (my custom field is a sub_field of a group), and then var_dump returns nothing at all. I'm fairly new to WP/PHP development, not sure what's going on here. Any help would be greatly appreciated!
<?php
if(have_rows('wine_profile')) :
while(have_rows('wine_profile')) : the_row();
$label = get_sub_field("taxonomy_label");
var_dump($label);
$wineProfiles = new WP_Query(array(
'posts_per_page' => '-1',
'post_type' => 'wines',
'tax_query' => array(
array(
'taxonomy' => 'labels',
'field' => 'slug',
'terms' => $label
)
),
'order' => 'DESC',
'orderby' => 'ID'
));
if ($wineProfiles->have_posts()) : while ($wineProfiles->have_posts()) : $wineProfiles->the_post();
get_template_part('includes/component', 'colorBlockLg');
get_template_part('includes/component', 'profile');
?>
<?php endwhile;
endif; endwhile; endif; ?>

Try to use -
$label = get_sub_field("taxonomy_label",get_the_ID());
var_dump($label);

I guess because you haven't started post loop!
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Put your code here //
} // end while
} // end if
?>
``

Related

WP Offset Not Working with Foreach loop

I'm currently working a wordpress loop to retrieve blog posts, their titles, featured image, date and category. With that said, I'm attempting to offset the loop begin on the 5th descending post because the previous 4 are referenced earlier on the page.
I have successfully offset the posts but it seems that I can't grab the category.
<?php
$post_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC',
'offset' => 4
);
$post_query = new WP_Query($post_args);
if ($post_query->have_posts() ):
$count = 1;
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => true
) );
while ( $post_query->have_posts() ) : $post_query->the_post();
$feat_img = wp_get_attachment_url( get_post_thumbnail_id() );
?>
<div class="col-sm-3 col-xs-6">
<div class="featured-img" style="background-image: url(<?php echo $feat_img; ?>)"
<?php the_date('F j Y', '<h6>', '</h6>'); ?>
<h3><?php the_title(); ?></h3>
<div class="category"><?php echo $terms->name; ?></div>
</div>
</div>
I tried a slightly different approach and was able to get each posts category using a foreach loop, followed by a while and if loop. While I successfully got each posts category, the offset wasn't cooperating. Perhaps I'm overthinking it. Here's my other attempt at this.
<?php
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => true,
) );
$count = 1;
foreach ( $terms as $term ) :
$post_args = array(
'offset' => 4,
'post_type' => 'post',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $term->slug
)
),
);
$post_query = null;
$post_query = new WP_Query($post_args);
if ( $post_query->have_posts() ) :
while ($post_query->have_posts() ) : $post_query->the_post();
$feat_img = wp_get_attachment_url( get_post_thumbnail_id() );
?>
Anyone mind lending a hand to help accomplish both tasks? Any input would be greatly appreciated. Thank you in advance.
you need to set "posts_per_page" to some other value than -1, it's explained well in documentation
https://codex.wordpress.org/Class_Reference/WP_Query
posts_per_page (int) - number of post to show per page (available
since Version 2.1, replaced showposts parameter). Use
'posts_per_page'=>-1 to show all posts (the 'offset' parameter is
ignored with a -1 value). Set the 'paged' parameter if pagination is
off after using this parameter. Note: if the query is in a feed,
wordpress overwrites this parameter with the stored 'posts_per_rss'
option. To reimpose the limit, try using the 'post_limits' filter, or
filter 'pre_option_posts_per_rss' and return -1

Display custom taxonomy in a loop Wordpress

Ok, this is probably an easy one. But I can't seem to figure it out for some reason.
I have a custom post type called: Beachevents.
There i have a couple of events. I also have a custom taxonomy called: Thema.
When making my beachevent pages (not posts) i created some types of thema's (themes). Like: Strand Spellen (the slug is strand-spellen).
Now I want to make a loop that display's only strand-spellen with thumbnail and all that stuff.
Does anyone know how I go about this?
I tryed some codes like these but do don't do the trick.
$args = array(
'post_type' => 'beachevents',
'posts_per_page'=> -1,
'tax_query' => array(
array(
'taxonomy' => 'strand-spellen',
'field' => 'slug',
'terms' => 'all'
)
)
);
$products = new WP_Query( $args );
if( $products->have_posts() ) {
while( $products->have_posts() ) {
$products->the_post();
?>
<div class='content'>
<h2><?php the_title(); ?></h2>
</div>
<?php
}
}
else {
echo 'There seems to be a problem, please try searching again or contact customer support!';
}
Thanks!
You're close!
In your tax_query, taxonomy needs to refer to 'beachevents' and terms needs to refer to 'strand-spellen'.
So, your code will look like this:
'tax_query' => array(
array(
'taxonomy' => 'thema',
'field' => 'slug',
'terms' => 'strand-spellen'
)
)
For more information on building your queries, you may find the WP_Query documentation useful - there's a section in there on taxonomy queries.
Thanks to Tim for his help. Here is my full code for people who encounter this same problem.
<?php $args = array(
'post_type' => 'beachevents',
'posts_per_page'=> -1,
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'thema',
'field' => 'slug',
'terms' => 'strand-spellen'
)
)
);
$products = new WP_Query( $args );
if( $products->have_posts() ) {
while( $products->have_posts() ) {
$products->the_post();
?>
<div class='content'>
<h2><?php the_title(); ?></h2>
</div>
<?php
}
}
else {
echo 'There seems to be a problem, please try searching again or contact customer support!';
} ?>
Including ordered by title and ASC. Hope I coded it correctly...

Wordpress nested loop, cannot remove meta_key from the query

I have two custom posts types A and B, linked together with a same custom taxonomy.
While looping through A posts with the "default" loop, I want for each A to get all B with the same taxonomy.
The code looks like this:
<?php if(have_posts()): while(have_posts()): the_post(); ?>
<?php
$A_Bs=get_the_terms( $post->ID, 'A_B');
?>
<?php if($A_Bs!=false && count($A_Bs)>0):?>
<?php
$A_B=$A_Bs[0];
$args = array(
'post_type' => 'B',
'tax_query' => array(
array(
'taxonomy' => 'A_B',
'field' => 'term_id',
'terms' => $A_B->term_id,
),
),
);
$loop = new WP_Query($args);
$saved_post=$post;
?>
<?php while ($loop->have_posts()) : $loop->the_post();?>
blabla
<?php endwhile;?>
<?php $post=$saved_post;?>
<?php endif;?>
<?php endwhile; endif;?>
But the sub-loop is always empty. The reason is, in the query_vars I have these two guys:
'meta_key' => string 'position' (length=8)
'orderby' => string 'meta_value_num' (length=14)
and I can't get rid of them. I never specified this ordering anywhere and my B posts don't have this custom field.
It's generating this line in the SQL query:
aaaa_postmeta.meta_key = 'position'
and prevent me to list the posts.
I tried to play with the $args, removing the tax_query and changing the post_type but it's always the same.
Thank you for your time !
Sorry I just realized after hours that I have the following thing in functions.php
function order_by_position($query){
if(is_post_type_archive( 'A')||is_post_type_archive( 'C')||is_post_type_archive( 'D')){
$query->query_vars['meta_key']="position";
$query->query_vars['orderby']="meta_value_num";
}
return $query;
}
add_action( 'pre_get_posts', 'order_by_position' );
It's much more logical now.
Sorry for disturbing.

Get Post from another post type that shares the same taxonomy?

I've got Two custom Post Types set up in Wordpress, One being called Products and the other Suppliers. Slugs for these are 'product' and one being 'supplier'.
These two post types share a custom taxonomy called Suppliers which slug is 'supplier-tax'.This then has lots of children which are the different suppliers.
Basically, What I am trying to do is when you are on a single post page for a product, I need to pull in a post for the supplier as well. I thought that the best way to do this with how I have set it up is to select the appropiate supplier from the supplier taxonomy when on the product page. And this then queries and gets post from the 'Supplier' Post ttype with the same selected Taxonomy.
I wrote this query which brings in posts from the taxonomy, however It needs me to tell it which taxonomy and which slug etc to bring in plus it doesnt query the different post type which makes it useless, however it was a start:
<?php
$the_query = new WP_Query( array(
'post_type' => 'product',
'tax_query' => array(
'taxonomy' => 'supplier-tax',
'field' => 'slug',
'terms' => 'supplier_1',
),
) );
while ( $the_query->have_posts() ) :
$the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_post_thumbnail('full'); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
I've tried to adapt and include parts from queries I've fond on previous sources but I can't seem to crack it. This is my attempt at it:
<?php
$terms = wp_get_post_terms( $post->ID, 'supplier-tax' );
if($terms){
$supplier_terms = array();
foreach ($terms as $term){
$supplier_terms[] = $term->slug;
}
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( array(
'post_type' => 'supplier',
'tax_query' => array(
array(
'taxonomy' => 'supplier-tax',
'field' => 'slug',
'terms' => $supplier_terms, //the taxonomy terms I'd like to dynamically query
'posts_per_page' => '-1'
),
),
'orderby' => 'title',
'order' => 'ASC'
) );
if ( have_posts() ): ?>
<?php while (have_posts() ) : the_post(); ?>
<?php the_title(); ?>"><?php the_title(); ?>
<?php endwhile; ?>
<?php endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
}
?>
Has anyone got any ideas on what I'm doing wrong or how I can make this work?
I managed to find a solution to my problem, whilst I don't think it is the cleanest markup, it works and does the job.
<?php
$the_query = new WP_Query( array(
'post_type' => 'product',
'tax_query' => array(
'taxonomy' => 'supplier-tax',
),
) );
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
$terms = get_the_terms( $post->ID, 'supplier-tax');
foreach ( $terms as $term ) {
$termID[] = $term->term_id;
}
echo $termID[0];
?>
<?php
$my_query2 = new WP_Query( array(
'post_type' => 'supplier',
'tax_query' => array(
'field' => 'slug',
'terms' => '$termID',
),
) ); ?>
<?php while ($my_query2->have_posts()) : $my_query2->the_post(); ?>
<?php the_title(); ?>
<?php the_post_thumbnail('full'); ?>
<?php the_content(); ?>
<?php endwhile; ?>

Wordpress Notice: Trying to get property of non-object

I have a custom post type set up on my blog and a special page for the taxonomy. On the taxonomy page I am getting the below error. Can anyone give me some tips on how to resolve this error?
The page loads fine and works as I would expect. But I get the below error if I have debug set to true. I would like to resolve this. I pasted the cost from the loop which is run two time on the page with different criteria.
Notice: Trying to get property of non-object in /home3/ans/public_html/wp-includes/post-template.php on line 29
Code:
<?php
query_single('dealers', 'publish', '1', $taxtype, $value);
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php
$address=get_post_meta($post->ID, 'wpcf-street_address', TRUE);
$city=get_post_meta($post->ID, 'wpcf-city', TRUE);
$state=get_post_meta($post->ID, 'wpcf-state_abbreviation', TRUE);
$zip=get_post_meta($post->ID, 'wpcf-zip_code', TRUE);
$phone=get_post_meta($post->ID, 'wpcf-phone_number', TRUE);
$paid=get_post_meta($post->ID, 'wpcf-paid', TRUE);
$post_id=get_the_ID();
get_each_dealer_brand($post_id);?>
<?php
echo "<ul class=\"ullisting\">";
if($paid==1)
{
echo "<li><p class=\"plisting\"><strong>";the_title();echo "</strong></p></li>";
echo "<li><p class=\"plisting\">$address | $city, $state $zip</p></li>";
echo "<li><p class=\"plisting\">P: $phone</p></li>";
echo "<li><p class=\"listing\"><span><small>$brands_list</small></span></p></li>";
}
echo "</ul>";
?>
<?php endwhile; ?>
<?php
wp_reset_query();
wp_reset_postdata();
unset($brands_list);
?>
This is the function referenced above:
function query_single($posttype, $poststatus, $paidvalue, $taxtype, $value) {
global $wp_query;
$wp_query = new WP_Query();
$args = array(
'post_type' => $posttype,
'post_status' => array($poststatus),
'orderby' => 'rand',
'posts_per_page' => 20,
'meta_query' => array(
array(
'key' => 'wpcf-paid',
'value' => array($paidvalue),
'compare' => 'IN',
)
),
'tax_query' => array(
array(
'taxonomy' => $taxtype,
'field' => 'slug',
'terms' => $value
)
)
);
return $wp_query->query($args);
}
This error will arise when you try to access posts inside your theme,
in page-template.php we have,
function get_the_ID() {
return get_post()->ID;
}
Whenever we are accessing posts we need to check the below condition and make sure it works as default, because we may not use wp_reset_postdata(); all the time so,
global $post;
//check if post is object otherwise you're not in singular post
if( !is_object($post) )
return;
//If Object
$somevariable = get_post_meta(get_the_ID(), $something->get_the_id(), TRUE);
Hope this helps. Thanks.
You can try to execute your commands within ACTION (because wordpress is already loaded normally at that time).
like this:
ADD_ACTION('init','your_function');
function your_function(){
YOUR CODES HEREEEEEEEEEEE............
}

Categories