Sort the category with custom field - php

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');

Related

How to add a custom block as a usual "product" to shop page / archive-product.php

I'm looking for a possibility to add a custom html block/post to the woocommerce "shop page" inside the products grid, as a product.
What I mean.. I have a grid of products on the "shop" page (archive-product) and I want to create a special post/page/html block with some text information, that will be inserted into the products grid as a one of "product", but with no price, with no title and unclickable. I've attached the screenshot of the final result I want to have, it's really self explaining - here it is exactly what I'm looking for.
As an idea probably I can create a special product with specific slug or title and the corresponding script with pre_get_posts hook will find this post/product and modify it to look like I need. I'm looking for some code/ideas how to insert this specific block/page/post into the archive-product page on some position in the grid. Thanks!
Thanks for help, guys! I've implemented the functionality I was looking for. I've found the corresponding loop in archive-product.php and as was suggested by JapanGuy, I've added a simple "if i equal let's say 5 then echo < li>[Custom block]< /li>" .
The original snippet from archive-product.php:
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
Modified code with inserted custom block:
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
if ($i == 5) {
echo "<li>[Custom block]</li>";
}
$i++;
?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
Im such simple way I can add any content to the created [Custom block] and have an usual products grid with extra custom designed block. I'm not very experienced programmer, so probably my code is not perfect, but it works. Thanks!
Edit: Previous code was wrong, changed it here
$i=0;
while ($row = mysqli_fetch_array($query))
{
if ($i == 2) {
echo "Cusom block";
}
echo "<p> Product block " . $row['column'] . " </p>";
$i++;
}
Creating WordPress Custom Post Archives : Hope this meets your requirement.
Custom Post Archives list your custom content. You probably already know the standard WordPress archives. So you can follow this to display both together .
Ref here : https://wp-types.com/documentation/user-guides/creating-wordpress-custom-post-archives/

Wordpress query_posts displayed by category custom field

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

magento get all products by attribute

I have displayed attribute filters in my catalog page. My client Requirement is, In some categories, attribute filter has no products that time i need to hide the empty attribute label. How can i achieve this Please help me.
"I need Product count for particular attribute that is shape,color and watts like that how can i get all products count by attribute, if i get count i will control the attribute label using that."
My code in catalog/layer/view.phtml:
<?php if($this->canShowBlock()): ?>
<?php // echo $this->getStateHtml() ?>
<?php if($this->canShowOptions()): ?>
<?php $_filters = $this->getFilters() ?>
<?php if($_filter->getItemsCount()):
<?php echo $this->__($_filter->getName()) ?>
<?php echo $_filter->getCount();?>
<?php echo $_filter->getHtml(); ?>
<?php endif;?>
<?php endif;?>
<?php endforeach; ?>
//Get product collection from "catalog/product" model
$collection = Mage::getModel('catalog/product')->getCollection();
//Fetch Most popular Featured products
$collection->addAttributeToSelect('most_popular');
$collection->addAttributeToSelect('featured_prod');
//You can check your query by:
$collection->getSelect();
Hope this may help you.... :)

Remove Category Uncategorized from Wordpress Loop

I want to list all post from a custom post type by category so I came up with this code (see below) but them I see this uncategorized category I want to remove. I tried in the query cat=$cat_id&exclude="1"&post_type=locations but it's not working. Any ideas? Thank You
<div class="entry-content">
<?php
$cats = get_categories();
foreach ($cats as $cat) {
$cat_id= $cat->term_id;
echo "<h2>".$cat->name."</h2>";
query_posts("cat=$cat_id&exclude="1"&post_type=locations");
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php ?>
<?php the_title(); ?>
<?php echo '<hr/>'; ?>
<?php endwhile; endif; ?>
<?php } ?>
<?php wp_reset_postdata(); ?>
</div><!-- .entry-content -->
This is quite old, but I stumbled across it, so in case it is relevant to anyone, here is what I believe the answer is:
Since you are specifying each category by ID, you are trying to exclude the category that you are specifying when it has a term ID of 1. So in the first iteration of the loop, you are saying
cat=1&exclude=1&post_type=locations
Since you are specifying exactly what you are excluding, I believe the "inclusive" query value outweights the "exclusive" query value. If this is the case, you can instead do it like this (and even save a query!):
$cats = get_categories();
foreach ( $cats as $cat ) {
$cat_id = $cat->term_id;
if ( $cat_id == 1 )
continue; // skip 'uncategorized'
echo "<h2>".$cat->name."</h2>";
query_posts("cat=$cat_id&post_type=locations");
?>{the rest}<?php
}
Please note, I don't recommend the use of query_posts, and instead get_posts() or new WP_Query should be used instead. Further reading:
https://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/
`query_posts( 'cat=-3' );`
// 3 is category id you want to exclude
see more ref. about query_posts QUERY POSTS
$args = array('exclude' => array(1));
$categories = get_categories($args);
foreach($categories as $category) {
echo $category->name;
}
Please try this I Hope this works. Thank you.
The problem are your quotes
try this:
query_posts("cat=$cat_id&exclude=1&post_type=locations");
Hope this helps you.
TRY THIS ;
Go to Appearance then click on Widget,
Once widget opened, Click on Primary Widget area,
You will see "Category"
Just click on,Hold your left mouse button and move to
"Inactive widget " Group.
When you go back your website, You will not see Uncategorized anymore. :-))

What is the proper way to query a Wordpress custom field

I have this working query that successfully gets the custom field data in my page template file:
<?php $featuredpost_cat = get_field('featured_category_id'); ?>
If I echo that out into the page I get "23" the value of the custom field, so I know that is working, what I want to do is grab that value and use it as a query parameter.
Farther down my page I have this:
<?php query_posts( $featuredpost_cat . '&posts_per_page=1'); if (have_posts()) : while (have_posts()) : the_post(); ?>
All that this does is ignore my variable and return the latest post on the site.
I hope this is clear enough.
== Edit ===
In case I am not being clear, I want to get a custom field which is a category ID from the page, then use it in a query on the page template.
So I set the field as category ID: 23 and then call it in my query_posts function so that I only return posts from that category.
Maybe the full page of code will help: template code
How about
<?php query_posts( 'cat='.$featuredpost_cat . '&posts_per_page=1'); if (have_posts()) : while (have_posts()) : the_post(); ?>
I assume that $featuredpost_cat is a category id
Sorry, I don't understand your second code example. Are you trying to use ternary operator to accomplish this?
query_posts('cat='.$featuredpost_cat . '&posts_per_page=1');
if (have_posts()){
while (have_posts()){
the_post();
}
}
What does query_posts() and the_post()do? If query_post() fetches the posts, have_post() checks the existance of posts and the_post() echoes them on the page, the code above should work. If this is not the case, please tell what the functions do.
Edit. Removed the question mark.

Categories