$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Post Filtered by Custom Field Value|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Post Filtered by Custom Field Value

matteradmin7PV0评论

I'm filtering posts by a custom field value and it works if its just one but I want to have it so that i can enter the different custom field values multiple times in a post and have it appear in multiple queries. This is what the WP_Query looks like:

$args = array(
    'posts_per_page' => '10',
    'post_type' => 'products',
    'meta_key' => 'Product Category',
    'meta_value' => 'INSPECTION',
    'order' => 'asc',
    'paged' => $paged
);
$the_query = new WP_query($args);

so essentially I want to have product category entered into a post multiple times with different values and for it to appear in multiple queries. At the moment it will only appear in one. I realize i can use specific custom post taxonomy and categories but I want to try and avoid that

I'm filtering posts by a custom field value and it works if its just one but I want to have it so that i can enter the different custom field values multiple times in a post and have it appear in multiple queries. This is what the WP_Query looks like:

$args = array(
    'posts_per_page' => '10',
    'post_type' => 'products',
    'meta_key' => 'Product Category',
    'meta_value' => 'INSPECTION',
    'order' => 'asc',
    'paged' => $paged
);
$the_query = new WP_query($args);

so essentially I want to have product category entered into a post multiple times with different values and for it to appear in multiple queries. At the moment it will only appear in one. I realize i can use specific custom post taxonomy and categories but I want to try and avoid that

Share Improve this question edited Nov 15, 2018 at 9:21 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Nov 15, 2018 at 9:06 Zayd BhyatZayd Bhyat 892 silver badges15 bronze badges 4
  • Why do you want to avoid taxonomies? They're designed specifically for what you're trying to do. – Jacob Peattie Commented Nov 15, 2018 at 9:37
  • I am already using taxonomies for another purposes on this custom post type – Zayd Bhyat Commented Nov 15, 2018 at 9:43
  • also the staff using this system and some our own internal scripts use the query results for some data processing. Anyway if there is no other way ill use custom taxonomies – Zayd Bhyat Commented Nov 15, 2018 at 9:45
  • You can have more than one custom taxonomy. No reason you can't have another one. No point reinventing the wheel. – Jacob Peattie Commented Nov 15, 2018 at 10:07
Add a comment  | 

1 Answer 1

Reset to default 0

Took the advice and decided not to reinvent the wheel. Created a taxonomy specific for products called Product categories. Added the following code to my functions.php.

add_action('init', 'products_categories', 0);

function products_categories(){
$labels = array ('name' => _x('Product Categories','taxonomy general name'),
                'singular_name' =>_x('Product Category','taxonomy singular name'),
                'serch_items' => __('Search Product Categories'),
                'popular_items' => ('Popular Product Categories'),
                'all_items' => __('All Product Categories'),
                'parent_item' => null,
                'parent_item_colon' => null,
                'edit_item' => __('Edit Product Category'),
                'update_item' => __('Update Product Category'),
                'add_new_item' => __('Add Product Category'),
                'new_item_name' => __('New Product Category'),
                'separate_items_with_commas' => __('Seperate Product Categories with commas'),
                'add_or_remove_items' => __('Add or remove Product Categories'),
                'choose_from_most_used' => __('Most Used Product Categories'),
                'menu_name' => __('Product Categories'),
                );

register_taxonomy('product_categories', 'products', array(
'hierarchical' => false,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var' => true,
    'rewrite' => array( 'slug' => 'product_category' ),
));
}

Now all I have to do is retrain my staff, rewrite some of my internal scripts and redo my queries T_T

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far