Hot answers tagged

29

Use unserialize() to convert it into an array. $mydata = 'a:5:{s:9:"engine_id";a:1:{i:0;s:9:"300000225";}s:15:"transmission_id";a:1:{i:0;s:6:"257691";}s:5:"plant";a:1:{i:0;s:23:"Oshawa, Ontario, Canada";}s:15:"Manufactured in";a:1:{i:0;s:6:"CANADA";}s:22:"Production Seq. Number";a:1:{i:0;s:6:"151411";}}'; $mydata = unserialize($mydata); echo $mydata['...


24

<?php if(count($related)) { echo "<div>Read More<ul>"; foreach($related as $id) { echo '<li><a href="'.get_permalink( $id ).'">'.get_the_title( $id ).'</a></li>';     }     echo "</ul></div>";   } ?>


20

$args = array( 'post_type' => 'testimonials', 'posts_per_page' => 4, 'orderby' => 'post__in', 'post__in' => array(883, 563, 568, 106) ); Using post__in within the orderby value it will honour the order of the array of IDs passed in post__in


20

wpdb's get_results method takes an optional second argument that lets you specify how the data is returned. The default return is an object. But you can also set it to... OBJECT - result will be output as a numerically indexed array of row objects. OBJECT_K - result will be output as an associative array of row objects, using first column's values as ...


14

Firstly, define your custom cron job schedules. add_filter('cron_schedules', array($this, 'cron_schedules')); public function cron_schedules($schedules){ $prefix = 'cron_';// Avoid conflict with other crons. Example Reference: cron_30_mins $schedule_options = array( '30_mins' => array( 'display' => '30 Minutes', ...


13

If you only want to add an extra class, then you should use wp_get_attachment_image. It has few extra params, and the last one is used for setting class names. Sample usage: <?php echo wp_get_attachment_image( get_the_ID(), 'thumbnail', "", ["class" => "my-custom-class"] ); ?> The main advantage of this aproach is that you will also get the whole ...


12

I created a Kint plugin that works really well. I also integrates with the Debug Bar plugin. You can download it at: http://wordpress.org/extend/plugins/kint-debugger/ There are a few functions to help you out with WordPress specific globals: dump_wp_query() dump_wp() dump_post() For printing arrays in a styled, collapsible format you would do the ...


11

use post__in key instead of include to get posts from the specific post ids. $args = array( 'post_type' => 'testimonials', 'posts_per_page' => 4, 'orderby' => 'ID', 'post__in' => array(883, 563, 568, 106), ); And to order posts by the given posts ids, you can use the following array. $args = array( 'post_type' => '...


11

You can use get_the_title() to return the current post title in the loop.


10

Normally you would just specify a meta_value, e.g.: $args = array( 'post_type' => 'business', 'meta_query' => array( array( 'key' => 'specials', 'value' => 'these are not the specials you are looking for - Obi Wan Kenobi' ) ) ); However because your post meta is ...


9

Filters work by calling each of the hooked callback functions (in priority order). The value to be filtered is passed to the first callback funtion. The returned value of that callback function is then passed to the second callback, and the returned value from that is passed onto third and so on, until all hooked callbacks have been fired. Whatever the last ...


9

When multiple meta values are used for same meta key, WordPress store the values in different database rows. So every array, is stored in one row. Initial Data Let's assume you have an array like this: $family = [ 'father' => [ 'John', 'Doe', 40 ], 'mother' => [ 'Jane', 'White', 39 ], 'child_1' => [ 'Luke', 'Doe', 5 ] ]; If you store ...


8

WP has a lot of nifty functions in the corners for such stuff. Try this: if ( !empty( $categories ) ) { $excluded_categories = wp_list_pluck( $categories, 'term_id'); $excluded_categories = implode(',', $excluded_categories); }


8

First can you please confirm that you don't have any caching plugins enabled? Caching plugins can interfere with cron jobs because your visitors are not served a live page but a cached version of your page. If you have a caching plugin enabled, you can choose one of your pages, add an exclussion to your caching plugin's settings for that page so that it is ...


8

You have to collect multiple fields under the same name like this … name="collect[1]" name="collect[2]" … and adjust your widget logic to this. Here is a very simple demo widget: <?php # -*- coding: utf-8 -*- /* Plugin Name: Store Options as array */ add_action( 'widgets_init', array ( 'T5_Array_Options_Widget', 'register' ) ); class ...


8

No, this won't work: <?php echo get_option('notice_data[Message]'); ?> Because get_option pulls whole option value by option_name, it doesn't pull by pieces of the serialized array. What you are asking for is a key (option_name) called literally notice_data[Message]. Assuming you've saved the option as notice_data you aren't going to get a match, and ...


7

You can install and use Krumo with WordPress (or any PHP app really). There's the Hikari Krumo plugin that simplifies use, though check the comments there, there's an error that you'll need to manually fix to get it working with latest WP. There's also the WordPress Debug Bar plugin which you might find useful. EDIT- Another option I've recently discovered, ...


7

You just need to use get_permalink( $id ) and get_the_title( $id ).


7

I figured out what I was doing wrong. A simple beginners mistake. Array_reverse was working properly, but I wasn't then reassigning the reversed array back to the $home_shows WP_Query, hence not seeing any change. Here is the answer, and my revised code. <?php $args = array( 'post_type' => 'show', ...


7

If you only want IDs, the query will consume much less memory if you use the fields parameter to just get that one field back in an array: function ids(){ $args = array( 'numberposts' => -1, 'post_type' => 'product', 'meta_key' => 'wppl_is_dax', 'meta_value' => '1' 'fields' => '...


6

@EAMann's answer is correct, but there's already a build in WordPress function for fetching all registered post types: get_post_types <?php // hook into init late, so everything is registered // you can also use get_post_types where ever. Any time after init is usually fine. add_action( 'init', 'wpse34410_init', 0, 99 ); function wpse34410_init() { ...


6

You are overwriting $post_ids variable on every while loop, never collecting them. That can be solved using $post_ids = array(); while (have_posts()) : the_post(); $post_ids[] = get_the_ID(); endwhile; var_dump($post_ids); // this is an array of ids However there is simpler way, you can skip the whle cycle and simply run: if( function_exists( '...


6

I am not going to give any specific code but I will give you Idea how it can be done without creating any custom database table. I am assuming one driver per truck Algorithm Create two html list, make them sortable. Each list items represents a post type and have ID data field. Once sorting is done get all ids in order. Make two array one contains truck ...


6

Try this, $categories_array = array(); $categories = get_categories(); foreach( $categories as $category ){ $categories_array[] = $category->term_id; } array( 'param_name' => 'category_id', 'type' => 'dropdown', 'value' => $categories_array, // here I'm stuck 'heading' => __('Category filter:', 'overmax'), ...


6

The solution was that I need to compare the literal value of $projectID, so that LIKE compares an exact string instead of just numbers. To make $projectID literal, it needs to be wrapped in quotes. So, I changed this line: 'value' => $projectID, to: 'value' => '"'.$projectID.'"', Which solves the problem.


6

@birgire get_sites() example is the correct way. I would have just added this as a comment, but don't have enough dang reputation! The only change, is that public accepts an integer, not a bool. See https://developer.wordpress.org/reference/functions/get_sites/ $mysites = get_sites( [ 'public' => 1, 'number' => 500, '...


6

WordPress has the wp_list_pluck function, which can be helpful here. We can make an array of just term IDs from the array of objects like: $term_ids = wp_list_pluck( $subcat_terms, 'term_id' ); Then we can check in_array: $this_id = 42; if( in_array( $this_id, $term_ids ) ){ // do something }


5

For this kind of stuff, I wrote REF (requires PHP 5.3). Among many other features, it displays contents of DocBlock comments and linkifies PHP-internal classes, methods and functions (links point to the PHP manual). It also handles WordPress functions (links point to queryposts.com). Here's the output of $GLOBALS at the setup stage of WordPress in HTML mode ...


5

Have you tried... <input type="text" name="my_options[option1]" value="<?php echo $options['option1']; ?>" /> <input type="text" name="my_options[option2]" value="<?php echo $options['option2']; ?>" /> <input type="text" name="my_options[option3]" value="<?php echo $options['option3']; ?>" />? I should say though (and ...


5

Searching inside a serialized array is difficult and inefficient-- ie. slow. The following pure SQL will do it: SELECT * FROM `wp_postmeta` WHERE `meta_key` LIKE 'daysonair' AND `meta_value` LIKE '%thursday%' LIMIT 0 , 100 Something like this should get WP_Query to do something similar: $args = array( 'post_type' => 'post', 'meta_query' => array(...


Only top voted, non community-wiki answers of a minimum length are eligible