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............
}
Related
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
?>
``
I want to echo the pages only with the custom taxonomy, but now i have drop down all pages what i have in custom post type.
<?php
$post_type_object = get_post_type_object('property');
if ($post) {
$parent_properties_dropdown_args = array(
'post_type' => 'property',
'property_status' => 'condominium-villas-project', // <<-- Here is the (property_status - is the custom taxonomy and condominium-villas-project is the custom taxonomy tag)
'selected' => $prop_data->post_parent,
'name' => 'parent_id',
'show_option_none' => __('Not selected'),
'echo' => 0,);
$parent_properties_dropdown = wp_dropdown_pages($parent_properties_dropdown_args);
if (! empty($parent_properties_dropdown)) {
?>
<div class="form-option parent-field-wrapper">
<label for=""></label>
<?php echo $parent_properties_dropdown; ?>
</div>
<?php
} }
But im anyway get all pages in custom post type 'property'. i need only 'property' with the taxonomy to show.
example:
now ->
- Property 1 - 'property_status' 'rent'
- Property 2 - 'property_status' 'sale'
- property 3 - 'property_status' 'condominium-villas-project'
i want to get only
- property 3 - 'property_status' 'condominium-villas-project'
Using what I think is your exact code (I am still confused by your use of $prop_data) you could use the following code to get what (I think) you are looking for. Add the code to your theme's function.php file, a .php file it requires, or one of the .php files of a plugin you might implement:
<?php
class wpse_51782342 {
static function on_load() {
add_filter( 'get_pages', [ __CLASS__, '_get_pages' ], 10, 2 );
}
static function _get_pages( $pages, $args ) {
if ( isset( $args[ 'property_status' ] ) ) {
$pages = get_posts(array(
'post_type' => 'property',
'posts_per_page' => -1,
'property_status' => $args[ 'property_status' ],
'include' => wp_list_pluck( $pages, 'ID' ),
));
}
return $pages;
}
}
wpse_51782342::on_load();
The 'get_pages' filter hook runs at the end of get_pages() which is called by wp_dropdown_pages(). In that hook the code looks for the argument you passed named 'property_status' to decide if it should modify behavior. This is an important technique because it ensures that the same args will always return the same results and are not dependent on something like the current post ID or URL. Following this principle will usually reduce the number of bugs you have to fix in your project.
If the argument 'property_status' is found the $args array the code uses its value to call get_posts() to return a list of posts that have been assigned the value of property_status that you passed to wp_dropdown_pages().
Finally the code limits the get_posts() query to the $post->IDs from $pages found by the query already run by wp_dropdown_pages(). This should result in a dropdown showing just the pages you prefer.
And for reference, here is the code in single.php to test out the above code, after I entered examples for property and property status, of course.
wp_dropdown_pages(array(
'post_type' => 'property',
'property_status' => 'condominium-villas-project',
'selected' => $post->ID,
'name' => 'ID',
'show_option_none' => __('Not selected'),
'echo' => 1,
));
Hope this helps?
You cannot filter wp_dropdown_pages() with custom taxonomy. You can use normal WordPress query like below.
<?php
$the_query = new WP_Query( array(
'post_type' => 'property',
'tax_query' => array(
array (
'taxonomy' => 'property_status',
'field' => 'slug',
'terms' => 'condominium-villas-project',
)
),
) );
if ( $the_query->have_posts() ) :
?>
<div class="form-option parent-field-wrapper">
<label for=""></label>
<select name='parent_id' id='parent_id'>
<option value="">Not selected</option>
<?php
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<option value="<?php the_ID(); ?>"><?php the_title(); ?></option>
<?php endwhile; ?>
</select>
</div>
<?php
endif;
wp_reset_postdata();
?>
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.
An example of the query_string would be: testdev.com/test-results/?Title=166
I have the following code below. What I can't figure out is how to set up the last array to get the ID and then produce the correct post accordingly. (the relation can be ignored, the other arrays I have are working and weren't needed for the example)
Any tips/help would be appreciated
<?php
**THIS IS WHERE I TRIED TO GET THE ID**
$queryTitle = $_GET['Title'];
$pageTitle = the_title();
$args = array(
// all your args here
'post_type' => 'resorts',
'meta_query' => array(
'relation' => 'OR',
array( **THIS IS WHERE I TRIED TO GET THE TITLE FROM THE URL**
'key' => $pageTitle,
'value' => $queryTitle,
'compare' => '=',
),
)
);
$query = new WP_Query( $args );
if($query->have_posts()) : while ($query->have_posts()): $query->the_post(); ?>
content etc goes here
<?php endwhile; else : ?>
<p>No results found, modify your search criteria and try again!</p>
<?php endif; ?>
get_page() could be used:
$queryTitle = $_GET['Title'];
$your_page = get_page($queryTitle);
echo $your_page->post_title;
I'm trying to make a Wordpress plugin for my blog that scans for posts that contain the custom field _videoembed. I've got everything made and it activates correctly but I receive a PHP error when I open up a page to test it out:
Fatal error: Call to a member function get_results() on a non-object in .../wp-content/plugins/youtubesubscription/videos.php on line 26.
Does anyone know enough about PHP to help me out? Here's my plugin code (pasted on pastebin because of size):
http://pastebin.com/uaEWjTn2
EDIT 1
I'm no longer getting any errors after inserting global $wpdb; but now nothing's showing up. Here's my updated code: http://pastebin.com/R2ZuEknY. Note, this code also incorporates auto YouTube thumbnails, which were obtained from a function that trims YouTube URLs to the IDs (link).
EDIT 2
Got it working, turns out all I needed to do was insert '_videoembed' as the 'meta_key' argument for a wp_query. Here's my working code below:
<?php
$args = array(
'meta_key' => '_videoembed',
'post_status' => 'publish',
'posts_per_page' => '' . $number . '',
'order' => 'date'
);
query_posts( $args );
while ( have_posts() ) : the_post(); ?>
<?php
global $post;
$VIDEOID = ytvideoID($post->ID);
?>
<li onClick="window.location.href='<?php the_permalink(); ?>'">
<?php
global $post;
$videoId = ytvideoID($post->ID);
$videoInfo = parseVideoEntry($videoId);
echo '<a href="'.get_permalink().'">';
echo '<div class="image">';
echo '<img src="http://img.youtube.com/vi/'.$VIDEOID.'/default.jpg">';
echo '<div id="time" style="position:absolute;z-index:9;bottom:2px;right:2px;font-size:10px;color:#fff;background:#000;padding:0px 2px;-webkit-border-radius: 4px;-moz-border-radius: 4px;border-radius: 4px;opacity:0.75;">'.hms($videoInfo->length).'</div>';
echo '</div>';
echo '<h4>'.get_the_title().'</h4>';
echo '<div id="description">';
echo '<div id"views"><h3>'.number_format($videoInfo->viewCount).' Views</h3></div>';
echo '<div class="singleauthor"><h3>by '.$videoInfo->author.'</h3></div>';
echo '</div>';
echo '</a>';
?>
</li>
<?php endwhile;
// Reset Query
wp_reset_query();
?>
you get the post by custom field using the meta_query.
$args= array(
'category_name' => 'courses',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'front_page',
'value' => 'yes',
'compare' => 'LIKE',
))
);
$the_query = new WP_Query( $args );
please refer my tutorial for more details.
http://www.pearlbells.co.uk/filter-posts-custom-fields-wp_query/
You'll have to to add global $wpdb;
try replacing
function mbrecentvids()
{ ?>
<?php
with
function mbrecentvids()
{
global $wpdb;
It looks like you're missing a semi-colon on line 26.
I see:
$pageposts = $wpdb->get_results($querydetails, OBJECT_K)