question
stringlengths 0
34.8k
| answer
stringlengths 0
28.3k
| title
stringlengths 7
150
| forum_tag
stringclasses 12
values |
---|---|---|---|
I've noticed that my process is rather slow and i'd like to speed it up. I want to be able to duplicate my database from my domain which is blueharlequin.com and work locally thus speeding up the process. The reason I want to do that is because our website has specific loops that I cannot duplicate without knowing the category ID as well as other things. I've already downloaded wp-db-backup and uploaded the .sql into PHPmyAdmin however everything I click will link me to my website which is not what I want. My question is how do I make it work locally? | I will suggest Wordpress Duplicator , It is best tool for copying your site to any other host/localhost, and its free, I have used it many times and it works every-time with different server settings. Please note that I haven't affiliated with it in any way. | Duplicate domain database to local - How? | wordpress |
In my post meta data I have the following 'key' for every post: "image_details" a typical value of which is an array such as: <code> a:3:{s:23:"info_window_bg_image_id";s:3:"128";s:12:"colour_start";s:7:"#000000";s:10:"colour_end";s:7:"#b7b7b7";} </code> In the above example - <code> info_window_bg_image_id </code> is equal to 128 Ideally I would like to find out how many posts have the <code> image_details['info_window_bg_image_id'] </code> set to 128 but I would settle to find out if any of the posts have that value set. The only way that I can think to do it (with my limited knowledge) is something along the lines of: Retrieve an array of every post in the database Foreach through the array using the ID's Use get_post_meta(ID, Key) to get the "image_details" key Check the value of the 'info_window_bg_image_id' element of the result to see if it matches the value. This seems like it would be a heavy load because I would have to do this multiple times to check for other values. Is there an easier way of doing this? Perhaps with a direct SQL query via the WP_Query class? Just to reiterate my ideal would be to get a result that lets me know there are: 6 posts have post_meta image_details 'info_window_bg_image_id' set to 128 10 posts have post_meta image_details 'info_window_bg_image_id' set to 127 0 posts have post_meta image_details 'info_window_bg_image_id' set to 126 etc etc But I would also be happy with a result that just says: 128 is used, 127 is used, 126 isn't used etc | You have a structural problem with your data. Serialized data in the database is terrible if you need to search over pieces of that serialized data. There is no reliable, efficient, and certainly no easy, SQL query to search over serialized data. stackexchange-url (""serialization" is a PHP mechanism. It isn't SQL. To the database that is just a string.") Your only SQL choice is a regex on the string. <code> WP_Query </code> certainly won't do it. That functionality is not built in, probably for the reason listed above. Iterating over the data is about the only solution you have given the data structure you are dealing with. However, the correct solution in my opinion is to alter your data structure so that you are saving granular pieces of data in the database. In other words, use a lot of different keys for the values that you need to search over. | How to count post meta key values for all posts in database | wordpress |
With WordPress 3.9 coming soon it's bringing along TinyMCE 4.0. I'm running my plugins and functions through some testing and found that one of my functions seems broken / not working with the new tinyMCE <code> function myformatTinyMCE( $in ) { $in['theme_advanced_buttons1'] = 'bold'; $in['theme_advanced_buttons2'] = 'formatselect'; $in['wordpress_adv_hidden'] = false; return $in; } add_filter( 'tiny_mce_before_init', 'myformatTinyMCE' ); </code> The above function used to limit the buttons in the tinyMCE to specific buttons, I've simplified it a bit to make it easier for testing. If I print out <code> $in </code> it shows the correct values in the correct spots, but when I actually load up a page it just defaults to the normal buttons. I've also tried to use the example in the TinyMCE Codex which also didn't seem to have any effect. I'm using the WordPress Beta Tester Plugin and I do have TinyMCE Advanced installed but disabled. With the new TinyMCE how can I continue using / customizing, and are there any 'Gotchas' that have changed from the previous TinyMCE Version? EDIT Looks like <code> $in['wordpress_adv_hidden'] = false; </code> is not longer being used - No idea how to unhide the Kitchen Sink. | The strings was new, not more for your requirements. This is the new content of the hook. <code> array ( 'selector' => '#content', 'resize' => 'vertical', 'menubar' => false, 'wpautop' => true, 'indent' => false, 'toolbar1' => 'template,|,bold,italic,strikethrough,bullist,numlist,blockquote,hr,alignleft,aligncenter,alignright,link,unlink,wp_more,spellchecker,wp_fullscreen,wp_adv', 'toolbar2' => 'formatselect,underline,alignjustify,forecolor,pastetext,removeformat,charmap,outdent,indent,undo,redo,wp_help', 'toolbar3' => '', 'toolbar4' => '', 'tabfocus_elements' => 'insert-media-button,save-post', 'body_class' => 'content post-type-post post-status-draft post-format-standard', ) </code> Also change the strings inside the array in your source to: <code> function myformatTinyMCE( $in ) { $in['toolbar1'] = 'bold'; $in['toolbar2'] = 'formatselect'; return $in; } add_filter( 'tiny_mce_before_init', 'myformatTinyMCE' ); </code> But see also this test plugin Gist 9758082 and stackexchange-url ("this thread") for WP 3.9 and the new TinyMCE 4.0 to understand the topic. Forcing Toolbar2 To show always the toolbar 2, without use the button <code> wp_adv </code> add the follow source to a plugin. <code> add_action( 'plugins_loaded', 'fb_force_show_toolbar2' ); function fb_force_show_toolbar2() { set_user_setting( 'hidetb', 1 ); } </code> BUT, now the hint for the value <code> wordpress_adv_hidden </code> . In the next WordPress version, after 3.9 will restore the old hook <code> wordpress_adv_hidden </code> to toggle the toolbar, see ticket 27963 . Then is possible to to use the follow source. <code> $in['wordpress_adv_hidden'] = FALSE; </code> <code> add_filter( 'tiny_mce_before_init', 'myformatTinyMCE' ); function myformatTinyMCE( $in ) { $in['wordpress_adv_hidden'] = FALSE; return $in; } </code> | WordPress 3.9 - Trouble Editing new TinyMCE | wordpress |
Editors note: Twitter ID and Google Analytics only serve as examples and are exchangeable. TL;DR I want to save external data in a user profile once or twice a day. I added two custom fields in my user profiles: Twitter_ID (I manually put it from the dashboard) test1 (external data got with php function) I would like to be able to do something like : Save once a day in <code> test1 </code> for each user profile the external data I get from <code> Twitter_ID </code> . The main goal : I am working on a panel which will display statistics of my user websites (external website). For that, I use the Google Analytics API. I save the Google Analytics profile number in a custom profile field. For the moment, I run the script (statistics) each time my users want to see them (in front end). And it takes few seconds to generate them. My statistics change approx once a day (It's not hourly stats, but daily stats). So, the idea was to save once a day the statistics to the user profiles. So, like that, it won't take time to generate them each time the user will load a stats page. The solutions : So, I started to think about different solutions. But the best would be to save the data without any action from the users. Or maybe when someone visits the website, it would save the data for everyone once the day with wp_cron. For the moment, the only thing I succeed was to save the data when a user update his profile. But it will save only his data, and not for everyone, so it's not what I want. EDIT I tried this : <code> add_filter('cron_schedules', 'new_interval'); function new_interval($interval) { $interval['minutes_1'] = array('interval' => 10*60, 'display' => 'Once 1 minute'); return $interval; } function InitiateMyCron() { if (!wp_next_scheduled('MyCronAction')) { wp_schedule_event(time(), 'minutes_1', 'MyCronAction'); } } function MyCronAction( $user_id ) { $twitter_id = $_POST['twitter_id']; $result = $twitter_id; if ( !current_user_can( 'edit_user', $user_id ) ) return false; update_usermeta( $user_id, 'test1', $result ); } add_action( 'personal_options_update', 'MyCronAction' ); add_action( 'edit_user_profile_update', 'MyCronAction' ); </code> But, the field "test1" is updated when I save the "Twitter_id". I don't know if it is updated every minute EDIT 2 <code> if ( ! wp_next_scheduled( 'my_save_statistiques' ) ) { wp_schedule_event( strtotime('tomorrow'), 'daily', 'my_save_statistiques' ); } function my_save_statistiques( $user_id ) { "blabla php" = $ga->requestReportData($num_analytics,array('pagePath'),array('visits'), null, null, $fromDate, $toDate); $visites_totales = $ga->getVisits(); if ( !current_user_can( 'edit_user', $user_id ) ) return false; update_usermeta( $user_id, 'visites_totales', $visites_totales ); add_action( 'personal_options_update', 'my_save_statistiques' ); add_action( 'edit_user_profile_update', 'my_save_statistiques' ); } </code> EDIT 3 I finally ended up with this but it's not working : (I changed 1 week to 30 seconds but the name are still the same) <code> add_filter( 'cron_schedules', 'myprefix_add_weekly_cron_schedule' ); function myprefix_add_weekly_cron_schedule( $schedules ) { $schedules['weekly'] = array( 'interval' => 30, // 30 seconds 'display' => __( 'Once Weekly' ), ); return $schedules; } // Schedule an action if it's not already scheduled if ( ! wp_next_scheduled( 'myprefix_my_cron_action' ) ) { wp_schedule_event( time(), 'weekly', 'myprefix_my_cron_action' ); } // Hook into that action that'll fire weekly add_action( 'myprefix_my_cron_action', 'myprefix_function_to_run' ); function myprefix_function_to_run() { function my_save_statistiques( $user_id ) { [...] $visites_totales = $ga->getVisits(); if ( !current_user_can( 'edit_user', $user_id ) ) return false; update_usermeta( $user_id, 'visites_totales', $visites_totales ); } add_action( 'personal_options_update', 'my_save_statistiques' ); add_action( 'edit_user_profile_update', 'my_save_statistiques' ); } </code> Edit 4: I discovered that add_action( 'personal_options_update', 'my_save_statistiques' ); is the action when a user updates his own profile but it's not what I want... Because I want to save the data for all users once a day. So that's why (I guess), I thought that my previous code wasn't working. Edit 5: FIRST STEP : 1/2 SUCCESS The user will save his data each time he visits the website. The main goal was to update the data for everyone but it can work like that. Next step : Cron <code> function my_save_statistiques( $user_id ) { $visites_totales = [myscript]; if ( !current_user_can( 'edit_user', $user_id ) ) return false; update_usermeta( $user_id, 'visites_totales', $visites_totales ); } add_action( 'wp_head', 'my_save_statistiques' ); </code> EDIT 6: Is it correct ? <code> // Schedule an action if it's not already scheduled if ( ! wp_next_scheduled( 'wp_head' ) ) { wp_schedule_event( time(), 'hourly', 'wp_head' ); } // Hook into that action that'll fire weekly function my_save_statistiques( $user_id ) { $visites_totales = [myscript]; update_usermeta( $user_id, 'visites_totales', $visites_totales ); } add_action( 'wp_head', 'my_save_statistiques' ); </code> I found a different solution The solution : Save the time when the action has been ran and check the difference with actual time : <code> function my_save_statistiques( $user_id ) { $user_id = get_current_user_id(); $current_time = time(); if (empty($current_time)) { update_usermeta( $user_id, 'last_analytics', $current_time ); } $last_analytics = get_user_meta( $user_id, 'last_analytics', true ); $diff = $current_time - $last_analytics; $one_day = 86400; if ($diff > $one_day) { $visites_totales = [myscript] update_usermeta( $user_id, 'last_analytics', $current_time ); update_usermeta( $user_id, 'visites_totales', $visites_totales ); } } add_action( 'wp_head', 'my_save_statistiques' ); </code> | An alternative way to do what I wanted : Save the time when the action has been ran and check the difference with actual time : <code> function my_save_statistiques( $user_id ) { $user_id = get_current_user_id(); $current_time = time(); if (empty($current_time)) { update_usermeta( $user_id, 'last_analytics', $current_time ); } $last_analytics = get_user_meta( $user_id, 'last_analytics', true ); $diff = $current_time - $last_analytics; $one_day = 86400; if ($diff > $one_day) { $visites_totales = [myscript] update_usermeta( $user_id, 'last_analytics', $current_time ); update_usermeta( $user_id, 'visites_totales', $visites_totales ); } } add_action( 'wp_head', 'my_save_statistiques' ); </code> So, the script will run for a logged user if the time between the last "run" and now is more than 24h , knowing that my website is only accessible to a logged user | WP Cron: Save third party data as user meta | wordpress |
I have a WordPress install on a dedicated server with decent memory (upwards of 8GB). The WordPress install includes many plugins - 50 active plugins. Most important to note is the WPML plugin to make WordPress multilingual. The theme is Weaver II Pro . The stack is LAMP - CentOS 6.3, Apache 2.4.4, Percona MySQL, PHP-FPM. I have been quite stupid, and didn't set up a test or staging server. Everything was running fine till I updated to WordPress 3.8, and also updated several plugins at the same time. I didn't pay attention at the time, but all my widgets got messed up, and so did any sort of navigation etc. I enabled error_log for a bit, i.e. <code> ini_set('log_errors',TRUE); ini_set('error_reporting', E_ALL); ini_set('error_log', dirname(__FILE__) . '/error_log.txt'); </code> This resulted in an immense file - basically the error_log grows by about a GB every 4 minutes or so. The primary error I found in the error log was database server disconnect, better known as MySQL server has gone away The first priority was to patch the problem. So I installed Database Ping plugin. This mostly , though not completely solved the issue. Many of the widgets came back. However, the special (non-English) characters are all messed up (converted to question marks). I tried to fix this by opening wp-mysql-ping.php and commenting out <code> if ( !DB_CHARSET && version_compare(mysql_get_server_info($this->dbh), '4.1.0', '>=')) { $this->query("SET NAMES '" . DB_CHARSET . "'"); } </code> Didn't help. Besides, the plugin is not a permanent or complete solution. So leaving the site in this messed up state, I started working on other possibilities: As suggested at Rackspace , I edited my.cnf to add <code> wait_timeout = 60 </code> Didn't help. Also added caching to my.cnf <code> query_cache_type = 1 query_cache_limit = 8M query_cache_size = 512M </code> Didn't help. (and yes, I restarted mysql every time I edited my.cnf) Also tried the method suggested here : In wp-db.php before <code> $this->select( $this->dbname, $this->dbh ); </code> I added <code> $this->query("set session wait_timeout=600" ); </code> Didn't help. I'm at my wit's end. Please help! | Fixed this by setting max_allowed_packet = 16M in my.cnf and restarting mysql | "MySQL server has gone away" since update to 3.8 | wordpress |
If I want to create a link that will always show the latest post how can i do that? Lets say I have three post types and there is three lists with posts names likes a nav list to those post types. How do I make it dynamic? I can't make it like this blah.com/post Because it always will be always changing. | If I understand well you want to show the last post (one post) from one of the 3 post types you have, using a dynamic url like <code> http://example.com/latest </code> . First of all lets add a filter to <code> 'do_parse_request' </code> filter: <code> add_filter( 'do_parse_request', function( $bool, WP $wp ) { $home_path = trim( parse_url( home_url(), PHP_URL_PATH ), '/' ); $path = substr( trim( add_query_arg( array() ), '/' ), strlen( $home_path ) ); if ( $path === 'latest' ) { // change 'latest' to anything else to change the url $wp->query_vars = array( 'posts_per_page' => 1, // if you want more than one post change this 'post_type' => array( 'post', 'another_cpy', // this should be your CPT name 'yet_another_cpt' // this should be your CPT name ) ); $wp->is_latests = true; return false; // stop WordPress parsing request } $wp->is_latests = false; return $bool; }, 0, 2); </code> Using previous code when you visit a page like <code> http://example.com/latest </code> WordPress will pick the last post from one of the 3 CPT, however you have no control on the template ( <code> index.php </code> will be used). However a simple filter to <code> 'template_include' </code> allows to choose the template: <code> add_filter( 'template_include', function( $template ) { global $wp; if ( $wp->is_latests ) { // change the name of the templates you want to use. // If none of the files is found in your theme folder, than index.php will be used $locate_template = locate_template( array( 'latest.php', 'single.php' ) ); if ( ! empty( $locate_template ) ) { $template = $locate_template; } } return $template; } ); </code> That's all. Note that: to view the page only one db query is required you do not need to create a page, nor a page template for the scope no rewrite rule is involved Edit If you want more than one post, let's say 3, and you want the last post for each CPT (like the answer by @DrewAPicture does) you can do it without running 3 separate queries. First of all change <code> 'posts_per_page' => 1 </code> to <code> 'posts_per_page' => 3 </code> in the code above, after that add a filter to <code> 'posts_groupby_request' </code> : <code> add_filter( 'posts_groupby_request', function( $groupby, $query ) { global $wp; if ( ! is_admin() && $query->is_main_query() && $wp->is_latests ) { $groupby = "{$GLOBALS['wpdb']->posts}.post_type"; } return $groupby; }, PHP_INT_MAX, 2); </code> | How do I create a link that will always show the latest post? | wordpress |
I am using horizontal-scrolling-announcement plugin to announce a news bellow the header.I have added some custom code to hide scrolling news announcement at one event if the admin change the status display no and its working .Before the customize the plugin if i set the status display no it was scrolling news with text "No announcement available.". Now the problem is that if the plugin updated automatically my custom will be overwrite with new code i just wanted know that how can i update the plugin without overwrite the my previous custom code. Bellow is the function which i have added some custom code to hide scrolling. <code> <?php function HSA_shortcode( $atts ) { if ( ! empty($data) ) { // Scroll new announcement } else //Hide scrolling bar { ?> <style> .breaking-news{ visibility:hidden; height:0; width:0; opacity:0; } </style> /*$what_marquee = $what_marquee . "No announcement available."; if($group <> "") { $what_marquee = $what_marquee . " Please check this group " . $group; } */ <?php } return $what_marquee; } </code> | i just wanted know that how can i update the plugin without overwrite the my previous custom code. You don't. Well, you do the same thing over and over and over again every time you want to update. Download the plugin. Hack your changes into it. Upload to your site. Repeat. Which is why you don't edit plugins that you didn't write and don't maintain . The plugin might contain an action or a filter that you can use, but I don't know about this particular plugin. There is another option in your case, I am pretty sure. Your function name is <code> HSA_shortcode </code> so I am assuming this is a shortcode callback. Shortcodes can be hijacked. Try: <code> function my_first_shortcode($atts) { return 'howdy'; } add_shortcode('hijack','my_first_shortcode'); echo do_shortcode('[hijack]'); function my_second_shortcode($atts) { return 'hijacked !'; } add_shortcode('hijack','my_second_shortcode'); echo do_shortcode('[hijack]'); </code> If you can find the original shortcode slug, you can put your function (probably with a different name) in your theme's <code> functions.php </code> or another plugin and hijack the orginal shortcode. <code> function my_HSA_shortcode( $atts ) { // ... } do_shortcode('origslug','my_HSA_shortcode'); </code> This, of course, will work only until the plugin author changes the shortcode "slug", but even then the maintenance hassle is greatly reduced. | How to update plugin without overwrite custom code | wordpress |
I followed the beginner steps for creating a child theme . The child theme was created and it appears as an option on my Themes page. But unlike the other Themes on the page (including its parent), there is no preview. The other themes ("Twenty Twelve," "Twenty Thirteen," and "Twenty Fourteen") are all listed with an image preview. My child theme ("Twenty Fourteen Child") does not have any image. The child theme can be applied without issue. This issue arises when I create child themes from other themes as well. Any ideas why this is happening and if I can resolve? | Unfortunately this isn't covered in that codex page. These previews of the themes aren't auto generated. You will need to take a screenshot of your theme, and upload that screenshot to your root folder as 'screenshot.png'. This screenshot will be automatically used then as a theme preview pic for your theme. I quote from the Theme Development page in the codex about screenshots for theme preview pic Create a screenshot for your theme. The screenshot should be named screenshot.png, and should be placed in the top level directory. The screenshot should accurately show the theme design and saved in PNG format. The recommended image size is 880x660. The screenshot will only be shown as 387x290, but the over double-sized image allows for high-resolution viewing on HiDPI displays. | Child theme preview missing | wordpress |
I have this code: <code> <?php // List subcategories of category '4' $subcategories = get_categories('&child_of=4'); foreach ($subcategories as $subcategory) { echo sprintf(' <a href="%s">%s</a> <span class="sep">•</span>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name)); } ?> </code> This code retrieves a list of subcategories from category id 4. I have added this code in post loop( <code> have_posts() </code> ). It is showing all tagged subcategories from that category on every post's box . What I want is to just show the subcategories from the category 4, which is tagged in that post. It shouldn't show all the subcategories from all categories tagged for all the posts. Thanks in advance | Here is a function I've recently used (modified version of the code found in the codex) to display a list of the categories a post is attached to. This function first gets the parent category to which the post belong, and that info is then fed back into <code> wp_list_categories </code> to remove the parent category and to get a list of the child categories belonging to that parent <code> <?php $taxonomy = 'category'; // get the term IDs assigned to post. $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); // separator between links $separator = ', '; $categories = get_the_category(); $parentid = $categories[0]->category_parent; if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) { $term_ids = implode( ',' , $post_terms ); $terms = wp_list_categories( 'title_li=&style=none&echo=0&child_of=' . $parentid . '&taxonomy=' . $taxonomy . '&include=' . $term_ids ); $terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator ); // display post categories echo $terms; } ?> </code> | Display a list of subcategories a post belongs to | wordpress |
I have some questions about the loop. I'm using "twentyfourteen" theme as an example. I'm creating 2 php files with basic loop. One is home.php, and one is a template page called sample-page.php . Both contains this code; <code> if( have_posts() ) : while( have_posts() ) : the_post(); the_content; endwhile; endif; </code> Nothing fancy, the only difference is I have template declaration on sample-page.php <code> /** * Template Name: Sample Page */ </code> According to WordPress Codex, when you set a "Blog" as a static front-page, a home.php is used as template, if it exists. The "Static Front-page" will use the custom page template, if it exists, in my example, it would use sample-page.php . Now here's my questions; Why is it that 2, identical php file produce different results? In my example, the "sample-page.php" will display the "PAGE CONTENT", while "home.php" will display "RECENT POSTS" Why do we need, "while" loop, if all we want is to display the "PAGE CONTENT"? Can't we just use <code> if( have_posts() ) : the_post(); the_content(); endif; </code> If we want to show "RECENT POSTS" in a template page. Why do we need to provide some queries first? These questions might seems unnecessary and don't really have a purpose. Even my friend told me, "As long as you know how to drive a car, you don't need to understand how the engine works." But still I want to know. I would really appreciate if someone can give me some answers about this. Even if you don't know the answer, I thank you for reading! Regards, Dan | This quite a broad question to answer. I'm not going to go into detail here and into core code, you'll need to go and read the links I'm going to provide. I'm going to try to keep my answer sweet and short and useful :-) Take the bundled theme twentyfourteen for example, when you open any page template, you'll find this piece of code <code> if( have_posts() ) : while( have_posts() ) : the_post(); <----loop elements----> endwhile; endif; </code> This is your loop, which basically displays the queried information from the main loop on the spesific template. That is all the loop does, it displayes what is retrieved by the main query (or custom query if there is one). What to display where is decided by the Template Hierarchy . Wordpress relies heavily on this hierarchy structure. If you have a look at how the main query works, ( go and read Query Overview ) you'll see that the main query uses this template hierarchy to decide which template to use and what information to retrieve from the database, this is why the information will be diffirent for, lets say, category.php and author.php, although your loop is exactly the same as you say As per your question Why is it that 2, identical php file produce different results? In my example, the "sample-page.php" will display the "PAGE CONTENT", while "home.php" will display "RECENT POSTS" The abovementioned info basically covers this part, aswell, the main query uses <code> post_type=page </code> when it makes a query on page templates, and <code> post_type=post </code> on home.php, that is one of the big differences Why do we need, "while" loop, if all we want is to display the "PAGE CONTENT" The <code> while() </code> loop is not necessary on page templates. You'll only have one post to display. If we want to show "RECENT POSTS" in a template page. Why do we need to provide some queries first? By default, the main query queries <code> post_type=page </code> for page templates, not <code> post-type=post </code> . That is why by default you cannot display "recent posts" on pages. For this you'll need to run a custom query with <code> WP_Query </code> or alter the main query using <code> pre_get_posts </code> EDIT You can also go and have a look at stackexchange-url ("my answer") for further info | The Loop in Static Page | wordpress |
I've created a simple loop. And I have the following array for the query: <code> $live_tags = array( 'tag' => 'live', 'showposts' => 5, 'post_type' => 'post', 'post_status' => 'publish', 'orderby' => 'date', 'order' => 'DESC' ); </code> Where it says <code> 'tag' => 'live' </code> , I need a logic that asks: <code> if ('tag' == 'live' || 'category' == 'candy') </code> But I am unsure how to do that within the WP_Query array. Anyone know how to retrieve posts from either tag, or category? | You need a <code> tax_query </code> . <code> $live_tags = array( 'posts_per_page' => 5, // showposts has been deprecated for a long time 'post_type' => 'post', 'post_status' => 'publish', 'tax_query' => array( 'relation' => 'OR', array( 'taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => 'live', ), array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => 'candy', ), ), 'orderby' => 'date', 'order' => 'DESC' ); $q = new WP_Query($live_tags); var_dump($q->request); </code> Note that both the <code> {tax} (string) </code> and <code> showposts </code> arguments have been deprecated for quite some time. I would not recommend using them. | Retrieve all posts within tag OR category? | wordpress |
I found several plugins (like this one and this one ) to get the ability to attach a picture to a WP user. But these plugins are not maintained any more. Since there's apparently no plugin that does it today, is there an easy way to do it manually? | I use a plugin called Avatar Manager for that function. The author has also done a tutorial on how to write your own avatar manager plugin, but unfortunately it is also a bit outdated and doesn't work anymore. Although the plugin states it is compatible up to 3.6.1, it is still working in 3.8.1. I know at this stage the plugin is maintained as I just recently submitted the Afrikaans translations to the author, and that was included in the last version released. Images are uploaded via the users profile page if "Anyone can upload" feature is enabled by admin. Images are stored the exact same way as any image uploaded via the media uploader, and images are shown in the the Media section in the back end. | Adding a photo to each WP user | wordpress |
Question not related anymore.. First I tought about the relation OR/ AND for results, but I believe I should work with 'operator' => 'NOT IN'. I would like to query the results that meet multiple taxonomies instead of displaying all results that meet 1 of the tax. New question has been made. <code> <?php function page_content($filters, $is_search = false, $pg = 1, $cat = -1, $post_type) { if (!$is_search) { $filters = explode(',', $filters); $query_args = array( 'posts_per_page' => 30, 'post_type' => $post_type, 'post_status' => 'publish', 'paged' => $pg ); $terms = array(); foreach ($filters as $f) { if (!empty($f) && is_numeric($f) && $f > 0) { $terms[] = $f; }} if (!empty($terms)) { if (!is_array($query_args['tax_query'])) $query_args['tax_query'] = array(); $query_args['tax_query'] = array( 'relation' => 'OR', // NOT WORKING, STILL SHOWS 'AND' RESULTS array( 'taxonomy' => $post_type . '_filters', 'terms' => $terms, 'field' => 'id', )); } $queried_items = new WP_Query($query_args); } else { global $wp_query; $queried_items = $wp_query; } foreach ($queried_items->posts as $p) { $taxonomy = str_replace('%#%', $p->post_type, '%#%_filters'); $p->taxonomy = array(); $pfields = array("fields" => "all"); $terms = wp_get_post_terms($p->ID, $taxonomy, $pfields); $assgined_terms = array(); if ($terms && is_array($terms) && !empty($terms)) { foreach ($terms as $term) { if (!in_array($term->parent, $assgined_terms)) { $p->taxonomy[] = $term; array_push($assgined_terms, $term->parent); }} } }} </code> | Per the Codex: relation (string) - The logical relationship between each inner taxonomy array when there is more than one. Possible values are 'AND', 'OR'. Do not use with a single inner taxonomy array. So you are on the right track, but your <code> tax_query </code> is wrong. <code> relation </code> is part of the outer <code> tax_query </code> array and not part of the top level query arguments. <code> $query_args['tax_query'] = array( 'relation' => 'OR', array( 'taxonomy' => $post_type . '_filters', 'terms' => $terms, 'field' => 'id', ) ); </code> | relation OR instead of AND - Filtered term ID's in loop | wordpress |
I'm using the <code> transition_post_status </code> hook to perform some operations after publishing a post. In some conditions I would like to show an error message in a red box under "Edit Post" and above "Post published": How can I do that? | I wouldn't use that hook. Here's why Try something like this using admin_notices . <code> function wpsites_admin_notice() { $screen = get_current_screen(); if( 'post' == $screen->post_type && 'edit' == $screen->base ){ ?> <div class="error"> <p><?php _e( 'Updated Demo Message!', 'wpsites' ); ?></p> </div> <?php }} add_action( 'admin_notices', 'wpsites_admin_notice' ); </code> Untested. | How to show an error message after publishing a post? | wordpress |
I am building my website on Wordpress and I have created my custom login page.But for a post when an user comments and other nonlogged see the comment with along with a line down "Login to reply".And when the user clicks on that"Login" from "Login to reply" it redirects to wp-login.php.So how to redirect users to custom login page when "login to reply" for a post is clicked on wordpress instead of wp-login.php. | You need to redirect everytime WordPress looks for <code> wp-login.php </code> . You can do the same by using the below in your active theme's <code> functions.php </code> file. <code> function redirect_login_page(){ // Store for checking if this page equals wp-login.php $page_viewed = basename( $_SERVER['REQUEST_URI'] ); // permalink to the custom login page $login_page = get_permalink( 'CUSTOM_LOGIN_PAGE_ID' ); if( $page_viewed == "wp-login.php" ) { wp_redirect( $login_page ); exit(); } } add_action( 'init','redirect_login_page' ); </code> | how redirect users to custom login page when "login to reply" is clicked? | wordpress |
I am using this template: http://demo.undsgn.com/studiofolio/front-page/ I tried to make some modifications because I need the homepage template to display an excerpt beneath each thumbnail. I copied the template called <code> front-page.php </code> , renamed it to <code> front-page-excerpt.php </code> and made my modifications and it worked. The problem is there is a framework behind it that controls which template is shown on the homepage. The framework is: http://aquagraphite.com/2011/09/slightly-modded-options-framework/ In the backend it looks like this: In the backend my new template isn't showing up, and I think I found the problem. The template is called in the backend options panel like this: <code> $querystr = " SELECT $wpdb->posts.* FROM $wpdb->posts, $wpdb->postmeta WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = '_wp_page_template' AND $wpdb->postmeta.meta_value = 'templates/front-page.php' AND $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'page'"; $pageposts = $wpdb->get_results($querystr, OBJECT); </code> When I change it to: <code> AND $wpdb->postmeta.meta_value = 'templates/front-page-excerpt.php' </code> My modified templates show up, but the original templates are not displayed. I tried to put both in it like this: <code> $querystr = " SELECT $wpdb->posts.* FROM $wpdb->posts, $wpdb->postmeta, $wpdb->postmeta AS mt1 WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = '_wp_page_template' AND $wpdb->postmeta.meta_value = 'templates/front-page.php' AND mt1.meta_key = '_wp_page_template' AND mt1.meta_value = 'templates/front-page-excerpt.php' AND $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'page'"; $pageposts = $wpdb->get_results($querystr, OBJECT); </code> It doesn't work at all. What am I doing wrong? | No idea why you're using clunky custom SQL, use a meta query instead. <code> $pageposts = get_posts( array( 'posts_per_page' => -1, 'post_type' => 'page', 'post_status' => 'publish', 'meta_query' => array( array( 'key' => '_wp_page_template', 'value' => array( 'templates/front-page-excerpt.php', 'templates/front-page.php', ) ) ), ) ); </code> | Query multiple meta key values for template | wordpress |