/    Sign up×
Community /Pin to ProfileBookmark

PHP WordPress, WP_Query custom attributes

Hello all,
It’s been a while since I have been on here and I am glad y’all are still going strong. I still see NogDog is here and he has helped me so freaking much in the past! AWESOME!
`
Anyways, I am passing a variable via the URL and have had no problem. However, the variable is being passed to a page template called ‘product’ which displays a single product out of a catalog. The variable being passed is the slug name of the product that was selected from the catalog.

My catalog is a widget I developed to display a gallery of my products and also their custom fields which can be changed within the widget area. ANYWAYS, this is NOT a POST so I can’t use the get_post() approach! I need to query one particular product out of my catalog and it’s attributes so I can display it singularly on a page.

ESSENTIALLY:
When viewer clicks product from a catalog, site sends the slug name of that product via the URL to the product page ([url]http://localhost:8888/wordpress/product/?id=my-product-name[/url]). Now I need to query my products in the catalog (not a post) and find “my-product-name” under my custom slug field I created in each item.

My code for the product page:

[CODE]
<?php
if(isset($wp_query->query_vars[‘id’])) {
$product = urldecode($wp_query->query_vars[‘id’]);
}

echo $product;
?>

<div class=”grid-catalog”>
<div class=”site-catalog”>
<ul>
<!– CATALOG –>
<?php
if( dynamic_sidebar(‘catalog’)) : else : endif;
?>
</ul>
</div>
</div>
?>
[/CODE]

As of now, this doesn’t do anything but take you to the products page and show you the entire catalog just as you were looking at on the previous page. My goal is to query this catalog and display only the product that is in the URL variable.

Anyways, here is my catalog plugin/widget code:

[CODE]
<?php
/* This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Copyright 2016 Brad Harrell Design
*/
/*
Plugin Name: Troph Catalog
Plugin URI: http://www.bradharrell.com
Description: Add Brad Harrell’s catalog widget to your theme
Version 1.0
Author: Brad
Author: http://www.bradharrell.com
*/

function troph_catalog_widget(){
register_widget(‘troph_catalog_widget’);
}

add_action(‘widgets_init’, ‘troph_catalog_widget’);

class troph_catalog_widget extends WP_Widget
{
function troph_catalog_widget(){
//process widget
$widget_options = array(
‘classname’=> ‘troph_catalog_widget_classname’,
‘description’=> ‘A simple widget to show a catalog.’,
);
$this->WP_Widget(‘troph_catalog_widget’, ‘Troph Catalog Widget’, $widget_options);
}
function form($instance){
//show widget form in admin panel
$default_settings = array(
‘title’=>”,
‘slug’=>”,
‘imgLink’ => ”,
‘imgAlt’=>”,
‘price’=>”,
‘productType’=>”,
);
$instance = wp_parse_args(
(array) $instance,
$default_settings
);

$title = $instance[‘title’];
$slug = $instance[‘slug’];
$imgLink = $instance[‘imgLink’];
$imgAlt = $instance[‘imgAlt’];
$price = $instance[‘price’];
$productType = $instance[‘productType’];

?>
<p>Title: <input class=”widefat”
name=”<?php echo $this->get_field_name(‘title’) ?>”
type=”text” value=”<?php echo esc_attr($title)?>”/>
</p>
<p>Slug: <input class=”widefat”
name=”<?php echo $this->get_field_name(‘slug’) ?>”
type=”text” value=”<?php echo esc_attr($slug)?>”/>
</p>
<p>Img Link: <input class=”widefat”
name=”<?php echo $this->get_field_name(‘imgLink’) ?>”
type=”text” value=”<?php echo esc_attr($imgLink)?>”/>
</p>
<p>Img Alt: <input class=”widefat”
name=”<?php echo $this->get_field_name(‘imgAlt’) ?>”
type=”text” value=”<?php echo esc_attr($imgAlt)?>”/>
</p>
<p>Price: <input class=”widefat”
name=”<?php echo $this->get_field_name(‘price’) ?>”
type=”text” value=”<?php echo esc_attr($price)?>”/>
</p>
<p>Product Type:
<select class=”widefat” name=”<?php echo $this->get_field_name(‘productType’) ?>”>
<option value=”awards”>Awards</option>
<option value=”collections”>Collections</option>
<option value=”gifts”>Gifts</option>
<option value=”stationery”>Stationery</option>
<option value=”custom”>Custom</option>
</select>
</p>

<?php

}
function update($new_instance, $old_instance)
{
//update widget settings
$instance = $old_instance;
$instance[‘title’] = $new_instance[‘title’];
$instance[‘slug’] = $new_instance[‘slug’];
$instance[‘imgLink’] = strip_tags($new_instance[‘imgLink’]);
$instance[‘imgAlt’] = $new_instance[‘imgAlt’];
$instance[‘price’] = $new_instance[‘price’];
$instance[‘productType’] = $new_instance[‘productType’];

return $instance;
}
function widget($args, $instance)
{
//display widget
extract($args);

$title = apply_filters(‘widget_title’, $instance[‘title’]);
$slug = empty($instance[‘slug’]) ? ” : $instance[‘slug’];
$imgLink = empty($instance[‘imgLink’]) ? ” : $instance[‘imgLink’];
$imgAlt = empty($instance[‘imgAlt’]) ? ” : $instance[‘imgAlt’];
$price = empty($instance[‘price’]) ? ” : $instance[‘price’];
$productType = empty($instance[‘productType’]) ? ” : $instance[‘productType’];

echo ‘
<li>
<a href=”product/index.php?id=geometria-rectangulus”>
<img src=”../wp-content/themes/trophyology/’.$imgLink.'”alt=”‘.$imgAlt.'”>
</a>
<i class=”grid”><a href=”#” class=”award-name”>’.$title.'<span class=”price”>’.$price.'</span></a></i>
<i class=”stream”><div class=”award-name”><a href=”#” class=”pin”></a><a href=”#” class=”title-link”>’.$title.'</a><span class=”price”>’.$price.'</span><a href=”#” class=”purchase”>Purchase</a><a href=”#” class=”details-link”>More Details</a></div></i>
</li>
‘;

}
}
?>
[/CODE]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 21.2016 — Welcome back.

I know very, very little about WordPress programming, so this is possibly useless blabbering on my part. Anyway...if I'm somewhere close to understanding things, it sort of sounds to me like you may want to add a function to that troph_catlog_widget class, let's call it show_product_by_name(). It would have one argument, $product_name. The function would then search the database(?) for that name, pull out whatever info you need if it finds it, and output it however you want. (If it doesn't find it, it would output whatever sort of "oops" stuff you want.)

But as to all the little details regarding how/where you would then call that function, I don't know enough about WP widgets to even hazard a guess. ?
Copy linkTweet thisAlerts:
@bradleybebadauthorJan 24.2016 — Hey Nogdog! I figured you would answer this. Yeah, WP can be kind of weird, but I am learning. I ended up not passing variables in the URL and using permalinks instead. Much easier and SEO friendly! Haven't worked with PHP in a while, so kind of rusty. However, it's all coming back now! Thanks for always helping me!

Brad
×

Success!

Help @bradleybebad spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...