Tag Info

New answers tagged

0

You can use isset() function to check if it has a value. In your case you can do like this : $width = get_post_meta( $post->ID, 'iswidth', true ); if( isset( $width ) ){ echo "<li>Width (px): ".$width."</li>"; }


3

There are approx. 10,000 items returned by the query. That's your problem right there. No matter what you do inside the loop, WordPress is still loading 10,000 post objects into memory. Batch it up and sprinkle a little magic in your query arguments: $args = array( 'fields' => 'ids', // MAGIC! Just get an array of id's, no objects ...


2

WordPress uses Custom Fields (also called post meta on technical side) for storage of arbitrary data, associated with the posts. Note that it is different from Taxonomies, which is also available for respective kind of grouping data. However, especially in case of custom fields, it doesn't provide much in terms of building custom interfaces for that ...


2

Yes there are lot of ways you can achieve this by using plugin or pragmatically This Plugin will create custom content types as well as custom fields for specific content type: https://wordpress.org/plugins/wck-custom-fields-and-custom-post-types-creator/ However you can also do this via pragmatically: ...


1

If you have a lot of categories it will be a pain to search everytime every category and check against the field value. I wrote something and I would love to know if it's correct or not 1st I wrote a function that get the option name from the value since wordpress core provide only a function that get the option value from name function ...


0

Have you look for a plugin called ACF (Advanced Custom Fields) the plugin can be found here: https://wordpress.org/plugins/advanced-custom-fields/ with this plugin you can add as many custom fields as you want and also there is the logic functionality in the plugin you can make dependencies. for example you want to create some additional custom field for ...


0

this is not a direct answer to your question but a line of code you would use as reference to work onto. here is a copy of what i use to search thru my table, i search between a data range and then limit it to only display instances of an entered usr_id $this->items = $wpdb->get_results($wpdb->prepare("SELECT * FROM $table_name WHERE `datec` ...


0

You need to check to see whether the $_POST data contains your custom field information before saving it: if (!empty($_POST["intro"])) { update_post_meta($post->ID, "intro", $_POST["intro"]); } Also, Wordpress will pass the post ID and post object into your callback if you ask it too. function save_intro($ID, $post){ //preserve the data in the ...


1

What is the exact meta_key and meta_value for the field I created using the function custom_user_profile_fields ? created_by and some user ID, for example: $args = array( 'meta_key' => 'created_by', 'meta_value' => 123, ) You could use a meta_query for more complex searches. $args = array( 'meta_query' => ...


0

All of the comparison operators you list operate on whole strings except for LIKE, NOT LIKE, REGEXP, NOT REGEXP,RLIKE. However, using those operators is going to create very inefficient queries, and potentially unreliable ones. You are fighting with some of the basic design of MySQL with what you are doing. Save your "availability" key as many times as you ...


0

You may be able to use the LIKE operator: $meta_args[] = array( 'key' => 'availability', 'value' => $availability, 'compare' => 'LIKE' ); In the codex you can see a list of comparison operators.


1

Okay anyone wanting to achieve this, the code works. Just save the values in the custom field without quotes. e.g. red, car <?php $list = get_field( "main_attributes" ); $array = explode(',', $list); $args = array( 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'style_atrributes', ...


0

Ok, after a little playtime, here is the answer: add_action('save_post', 'ocp_jobs_save_details'); function ocp_jobs_save_details(){ global $post; $genref = sanitize_title( dechex( time() ) ); if ( $post->post_type == 'job' ) { if ( $_POST['ocp_jobs_ref'] ) { $jobs_args = array( 'ID' => $post->ID, 'post_name' => strtolower( ...


0

Use the wp_insert_post hook to run when a post is saved: function wpse_188435_save_movie_data( $post_id ) { if ( $imdb = get_field( 'imdb_id', $post_id ) ) { if ( $imdbInfo = get_imdb_connector_movie( $imdb ) ) { if ( isset( $imdbInfo['actors'] ) ) update_post_meta( $post_id, 'actors', $imdbInfo['actors'] ); ...


1

As @TheDeadMedic mentioned the API will exclude any meta with underscore. It's not a good idea to remove the underscore as it might break something in your code or dependencies, however you can try adding a code like this in your functions.php to unprotect the desired meta add_filter( 'is_protected_meta', 'wp692_meta_unprotect', 10, 2 ); function ...


0

I think getting ALL the posts with the city = Melbourne and then checking ALL of them against ALL the posts that you are attempting to show on the page is the wrong way about it. Why not use the single loop that you should already have and put something like this to check if the city is Melbourne? <?php //check custom field 'city' $custom_field ...


0

Unfortunately, that isn't how WP_Query works. As soon as you add that "meta" component, you've created a kind of filter. Dump $query->request and you will see what I mean. Second, WP_Query doesn't support ordering by a meta key at all. You can order by a meta value for a particular key but no by the key itself. Again, dump the query to see what I mean. ...


3

As stated in @ambroseya's answer, its supposed to work like that. Once you declare a meta query, even if you aren't looking for a specific value, it will only query posts with that meta key declared. If you want to include all posts sort them by the meta key, use the following code: $args = array( 'post_type' => 'news', 'orderby' => ...


1

That's actually how it works. If you want to do that without adding table rows, you'll have to do two queries. One with the meta_key that has the limited results, and the other that gets the whole list; then use PHP to compare the two query results (possibly removing the meta_key results from the other query to remove duplicates, or whatever makes sense in ...


0

You are missing "foreach" loop. Try this way $latest_cpt = get_posts("post_type=event"); foreach ( $latest_cpt as $cpt_post ) { $theidone =$cpt_post->ID; $this_post_id = $theidone; //get_the_ID(); $key_2_value = get_post_meta( $this_post_id, 'custom_select', true ); if( ! empty( $key_2_value )) { $thisisworking = $key_2_value ...


0

This is a code I'm using ina website that I'm creating for a client. Maybe it can point you in the right direction: $current_date = date('Y-m-d'); $args = array( 'post_type' => 'billboard_evento', 'posts_per_page' => 4, 'meta_key' => 'fecha-inicio', //Declaring wich meta key I want to use 'orderby' ...


3

There is a dynamic hook right before any new metadata is added: do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value ); It would be add_post_meta for posts accordingly. Note that you might to tinker some to catch this precisely in the case of new post being created, for example hook into publish_post first, note the ID, and add to this ...


0

Does this do the trick? $consignment= get_post_meta($post->ID, 'availability', true); if ($consignment == '') { // code to run if the above is empty, eg. echo "avaliable"; } else { // else the code, eg. echo $consignment; }


1

Best thing to do is to write your own CSV import php file. By using functions like wp_insert_post() you can insert the data from the csv file as a new post in the wordpress database. If you're not a coder, which I'm sensing, you can try this plugin: https://wordpress.org/plugins/wp-ultimate-csv-importer/. The free function offers everything you're ...


0

Figured it out. I had to re-read the codex a lot to figure out that when you pass an array to update_post_meta it is serialized. By using maybe_unserialize() on the return value I was able to to get access to the values. Hope this helps some future searcher!


0

Always use esc_attr to escape HTML field value attributes $cf = esc_attr(get_post_meta( get_the_ID(), 'custom_field', true )); echo '<a href="http://' . str_replace("http://", "", $cf) . '">' . $cf . '</a>';


0

I posted a working solution as of WordPress 4.2 here: Adding Sticky functionality to Custom Post Type Archives Basically, it implies installing a small plugin and add a code snippet.



Top 50 recent answers are included