get all pages page by custom field - wordpress

I am try to get all my page in wordpress that have a custom field like this.
$query = new WP_Query(array('meta_key=showHome&meta_value=true&post_type=page'));
print_r(count($query));
But this return only one element!
any idea to how get all pages with meta_key=showHome

remove meta_value=true from query string
$query = new WP_Query(array('meta_key=showHome&post_type=page'));
print_r(count($query));

Related

Adding a custom query argument to a custom taxonomy term page

I have a custom taxonomy created and I'm trying to add a new query argument with add_query_arg(), but it's not recognizing it at all when I visit the URL of a term of that taxonomy, with a query in the URL
for example, for a playlist taxonomy and the "edm" term and this URL:
http://someurl.com/playlist/edm/?some_variable=abc
some_variable is not recognized at all
When I use this code, to show all the query vars:
global $wp_query;
print_r($wp_query->query_vars);
some_variable doesn't exist, at all. It's like it doesn't see it at all!
You want to implement a hook somewhere in your plugin or theme so that WP is aware of your new variable.
public function my_plugin_add_query_vars($vars) {
$vars[] = "some_variable";
return $vars;
}

Drupal 7: Populate node fields with values from $form

I would like to know if it's possible to populate a node fields with the values from form_state or something like that.
Basically what I do is to show a register form merged with a form from a Content Type. I do that by using field_attach_form(). Now on submit I create a node using:
$node = new stdClass();
$node->type = 'company';
$node->uid = 1;
node_object_prepare($node);
and now I would like to get all values from form_state and put them into the node.
Many thanks!
Hook your form, add new submit handler and place your code there.

Doctrine Translatable how to only fetch records with a translation

I'm using Symfony2 + Doctrine + Translatable from DoctrineExtensions.
I have an entity article which has a couple of translatable fields like Title and Content. What is happening now is that I've created a bunch of articles in Chinese language, but have not added a translation for English. This is giving me some problems when I try to display a top 8 most recent articles on my homepage... When I view my homepage in English, it will show a bunch of title-less articles there (the Chinese articles that dont have a translation in English).
I found a solution for this in the Translatable extension by using ORM query Hint:
"\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER".
This lets you directly query the translated fields of your entity which are actually stored in ext_translations and not in the entity's table.
As a result the code I use to fetch the articles for the homepage looks like this:
public function getNewestArticles($limit = 1){
$dql =
"SELECT a FROM NewsBundle:Article a
WHERE a.published = 1
AND a.title != ''
ORDER BY a.created_at DESC";
$query = $this->_em->createQuery($dql);
$query->setMaxResults($limit);
$query->setHint(
\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
);
$result = $query->getResult();
return $result;
}
This actually works, but the generated query is so inefficient that it slows down the page a lot... I'm talking 1800 ms for that query. I've also tried to just put the fields I need in the select statement, which improves the performance, but still not enough.
Any ideas?
you can try to optimize the query manually. use locale as the parameter in your repository
public function getNewestArticles($locale, $limit = 1){
//your own superfast query
}
in the controller then set the locale with
$locale = $request->getLocale();
$em->getNewestArticles($locale, $limit);
additional question: have you set indexes on your tables?

Wordpress custom field in post database

I have added a field in wordpress posts table named "parent_location". How to get the value of this field in db front end? I have tried get_post_custom_values('parent_location', $post_id) function but it not working. Can any body help me out?
Thanks in advance, don't down vote
You added the field as a new column in wp_posts or added a new metadata entry to a single post? If its to a post then just do a get_post_meta(postID, 'parent_location', true); It its a new column into wp_posts, then should you not be able to just to a standard;
<?php
$my_id = 100;
$post = get_post($my_id);
$parent_loc = $post->parent_location;
?>
Not sure if that will work, I've not tried to get a custom column via that method before. If it doesn't, just roll your own SQL query; http://codex.wordpress.org/Class_Reference/wpdb

How to list a form's field-names

I have a form, and want to generate a list of the form's field-names. Here is how I currently do it:
$fieldnames = array();
foreach ($form as $key=>$val){
if (substr($key, 0, 6) === 'field_'){
$fieldnames[] = $key;
}
}
Is there a better way to do this?
UPDATE:
Just to clarify ... I am wondering whether there is a less "kludgey" way of doing this. For example, does the content module provide an api function that loops through fields. (I couldn't find one.)
the field that you added by cck...or from UI field system are begin with "field_"
and this fields are usually in the nodes...so if you are talkin about nodes form
and fields that added by cck....you are in the correct way... but if this fields are added programmatically....so you are in the wrong way
sorry im not 100% sure but i don't think you can get all the fields that added programatically..but if you added this fields from cck or from '/admin/content/node-type/stores/fields' where {stores} is your content type that you are working with then you can get this fields name from {content_node_field_instance} table as the following
$result_handle = db_query("select field_name from {content_node_field_instance} where
`type_name` = '%s'","yourContentTypeName") ;
while($result_object = db_fetch_object($result_handle)){
$fields[] = result_object->field_name ;
}
now you have the array $fields which hav all the fields of your content type...i hope that will help you

Resources