Query Product categories what have a custom field

Im trying to build a page template on wordpress that will show a list a categories that have a custom field checked.

So the taxonomy is just the default woocommerce taxonomy product_cat I then have a custom field to the product_cat taxonomy which is a check box called collectable

My set up is
Main Category

  • Child
  • Child
  • Child
  • Child

So for example 2 children categories have the tick box collectable which is set to add the value of 1 into the datbase

So I am doing a page where it will show all categories with the collectable checked.

$args = array(
    'post-type' => 'product',
    'taxonomy' => 'product_cat',
    'hide_empty' => 0
  );
  $c = get_categories($args);
  $c_keep = array();
  foreach($c as  $cat){
 if (get_field('collectable', 'category_'.$cat->term_id)) {
      $c_keep[] = $cat;
    }  
  }
  
  foreach($c_keep as $cat){
    echo $cat->name;
  }

But I am getting nothing at all returned.

I even put a

print_r( $args );

But I am still coming up with a blank (Header and footer Loads and the text above the query)

Can anyone help please

Go to Source
Author: user1348927

WP Custom fields – Add class to button element

I have looked into similar discussions in the forum. Most circle around my issue but non gave a definitive answer.

Working on an Intranet site running on WP (v. 4.8.14). I have been trying to style banners appearing on the home page. The intent is, each banner would appear in different colour combination. Each banner leads to a post type: Page. It is also intended once the code works to integrate it in the The Loop for those banners.

Banners on home page

I have been trying to get my head around WP custom fields and found the resource below, that seemed easy enough:

Style Individual Posts Using Custom Fields

Have added a Custom Field Key: cssclass with Value: bnr-yellow. This is a CSS class with some basic (for now) colour styling.

.bnr-yellow {
background: rgba(250, 240, 72, 1)!important; 
color: rgba(57, 185, 186, 1);}

The class itself works as I tested it manually from the Inspector.

Custom Field with value bnr-yellow

Updated (saved) the Page.

The issue

However I have been unable to make it work.
Although the code below returns all (possible) custom fields as a class, it does not return mine.

Image demonstrates missing class

The Image above demonstrates the returning output of the code below. Note the button requires .col-sm and .hmBanner which are passing through alright, (as evident).

Basic Loop…

<?php $custom_values = get_post_meta($post->ID, 'cssclass'); ?>

…and the button.

<button onClick="window.location.href='<?php the_permalink() ?>'" type="button" <?php post_class('col-sm hmBanner ' . $custom_values); ?> >
    <span>
        <i class="hmBannerImg animate__animated animate__backInDown " style="background-image: url(<?php echo wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ); ?>)"></i>
<div class="hvr-wobble-vertical brTtlDiv">
    <h3><?php the_title(); ?></h3>
</div>
        </span>

Have tried various iterations of the PHP, for example alter the code, as per a web resource, but same issue…

<?php $customField = get_post_custom_values("cssclass"); ?>

Regarding the post ID, I am not interested in it… So I have not been using it. Though if it proves it is needed for the function then I will add it.

What possibly am I missing here?

Any point to the right direction would be greatly appreciated.

Please help!…
Thank you for your time in advance.

P.S. I am not a novice nor am I clever.

Go to Source
Author: Jason

Randomly sort part of already sorted query

I’m not entirely sure how to word my question, but I’ll give it a go.

Basically, what I’m wanting to do is order a group of posts by a custom field. I have a True/False custom field, created using the ACF plugin, which is displayed on a certain custom post type.

Right now, I’ve set up a query to firstly display posts with the True/False statement set to true, and then to display the posts with the statement set to false. This is working well.

What I want to do, however, is take the posts with the true statement and randomly change their order. All while keeping the current order or True first and then False in place.

Here is my current query.

                $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                
                // the query
                $wpb_all_query = new WP_Query(array('post_type'=>'businesses', 'post_status'=>'publish', 'category_name'=>basename(get_permalink()), 'meta_key'=>'featured', 'orderby'=>'meta_value','order'=>'DESC', 'posts_per_page'=>20, 'paged' => $paged)); ?>

                    <?php if ( $wpb_all_query->have_posts() ) : ?>
                
                
                    <!-- the loop -->
                    <?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>

Go to Source
Author: Timothy Davis

Adding Custom Endpoint in WordPress Rest API

I have been working on custom WordPress rest API Endpoint. Goal is to create a WordPress custom route in this route i want to get Category Id and convert it to Category Name. I have written the function but its returning null for the category Id. The function simply get the categories of the WordPress and register the route.How Can I get the all Categories Id and convert category Id into category name.

{

    $categories = get_categories();

    $data = [];
    $i = 0;

    foreach ($categories as $category) {

        $data[$i]['id'] = $category->ID;
        $i++;
    }

    return $data;
}
add_action('rest_api_init', function () {
    register_rest_route('w/v2', 'trending', [
        'methods' => 'GET',
        'callback' => 'w_categories',
    ]);
});```

Go to Source
Author: Shahryar Rafique